python - Pandas 选择 n 中间行

标签 python pandas dataframe

假设我有一个数据框,像这样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/

相关文章:

arrays - 将数组内容推送到 DataFrame - Julia

r - 如何重复空行以使每个拆分具有相同的编号

python - 通过与另一个列表匹配来检索列表中最长的匹配值 [Python 2.7]

python - 为什么 np.percentile 对高百分位数返回 NaN?

python - 如何使用Python中另一列的值填充pandas数据框中的空值?

python - 使用 .agg() 进行 pandas DataFrame 多项操作的进度条

python - 绘制多索引 DataFrame 条形图,其中颜色由类别决定

python - 有条件地在数据框中填充一个值

python - 同一列中具有不同格式的 Pandas 日期时间

python - 是否有适用于大数据的 for 循环或任何其他循环的动态代码?