pandas 熊猫选择 n 中间行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46380075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas select n middle rows
提问by asdlfkjlkj
Suppose I have a dataframe, dflike this
假设我有一个数据框,df像这样
col1 col2 col3
1 2 34
11 32 32
21 62 34
31 12 31
13 82 35
11 32 33
41 32 33
and I want to select 3 rows after first 2 rows, that is I want to select these rows
我想在前 2 行之后选择 3 行,也就是说我想选择这些行
21 62 34
31 12 31
13 82 35
How can I do this?
我怎样才能做到这一点?
回答by Bharath
Use slicing of rows with loc
to do that like df.loc[2:5]
使用切片行loc
来做到这一点df.loc[2:5]
Output:
输出:
col1 col2 col3 2 21 62 34 3 31 12 31 4 13 82 35 5 11 32 33
If you want to ignore the current index then use slicing with iloc
which will get the rows between the range.
如果您想忽略当前索引,请使用切片iloc
来获取范围之间的行。
df.iloc[2:4]
col1 col2 col3 2 21 62 34 3 31 12 31
回答by Daniel Severo
You can do df.iloc[2:4]
or just df[2:4]
.
你可以做df.iloc[2:4]
或只是df[2:4]
。