【问题标题】:How to drop a row based on the condition of a column in pandas?如何根据熊猫中列的条件删除一行?
【发布时间】:2020-09-28 17:32:56
【问题描述】:
  • 我正在尝试删除2 列中包含'CENTER' 的行。

作品:

BuildingNameContains_center = dframe[dframe[2].str.contains('CENTER')]

输出作品:

BuildingNameContains_center

产生错误:

dframe.drop(BuildingNameContains_center, inplace= True)

KeyError: '[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23\n 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41] not found in axis'

【问题讨论】:

    标签: python pandas keyerror


    【解决方案1】:

    IIUC,而不是对数据框进行子集化,然后尝试删除行。只需对原始选择的逆进行子集化即可。

    dframe = dframe.loc[~dframe[2].str.contains('CENTER'), :]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-15
      • 1970-01-01
      • 2019-04-19
      • 2016-12-25
      • 2021-04-14
      • 2015-11-07
      • 2021-12-18
      • 1970-01-01
      相关资源
      最近更新 更多