【问题标题】:How to filter out a row in the middle of a pandas data frame by index position如何按索引位置过滤掉熊猫数据框中间的一行
【发布时间】:2020-06-18 14:17:36
【问题描述】:

我有一个包含 20,000 个观察值的 pandas 数据框。我只想过滤掉观察 879。我尝试了以下方法:

df.iloc[-879, ]

这只是产生了列名。如何取出数据框中间的特定列?

建议如下:如何从数据框的单元格中获取值?

它没有回答问题。它显示了如何根据按列值过滤从单元格中提取值。我需要在数据框中间按索引位置和一行进行过滤。

所以我需要索引 [0:878] 和索引 [880:]

【问题讨论】:

  • df.drop(df.index[879])?
  • 我收到了TypeError: 'RangeIndex' object is not callable
  • 不。有效。我使用() 而不是[]。我的错。谢谢!
  • 不需要额外的-,应该是[879]

标签: python pandas


【解决方案1】:

你可以试试np.r_:

df.iloc[np.r_[:879, 880:]]

drop:

df.drop(df.index[879])

【讨论】:

    【解决方案2】:

    你可以试试这样的。而不是 1 插入您想要的索引来过滤掉。

    import pandas as pd
    df = pd.DataFrame( {'a':[2,4,6], 'b':[7,3,5], 'c':[1,2,7] } )
    row_ilocs = [ x for x in range( df.shape[1]) if x != 1 ]
    df = df.iloc[ row_ilocs, :  ]
    

    【讨论】:

      猜你喜欢
      • 2015-05-28
      • 1970-01-01
      • 2021-08-01
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 2019-04-13
      相关资源
      最近更新 更多