假设我有一个数据框,像这样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
我想选择前两行之后的三行,也就是我想选择这些行
21 62 34
31 12 31
13 82 35
我该怎么做?
最佳答案
使用 loc
对行进行切片,如 df.loc[2:5]
输出:
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.
df.iloc[2:4]
col1 col2 col3 2 21 62 34 3 31 12 31
关于python - Pandas 选择 n 中间行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46380075/