【问题标题】:KeyError: "[Column] not found in axis" [duplicate]KeyError:“在轴中找不到[列]”[重复]
【发布时间】:2019-12-17 11:17:29
【问题描述】:

如果在 2 个数据框之间存在一列,我想删除它。我检查它是否存在,然后尝试删除它,但它说找不到。

for column in positionsdf.columns:
    if column in entrydf.columns:
        entrydf = entrydf.drop(column)

错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-20-d9893719abf8> in <module>
      1 for column in positionsdf.columns:
      2     if column in entrydf.columns:
----> 3         entrydf = entrydf.drop(column)

...
...
...

KeyError: "['caseworker'] not found in axis"

【问题讨论】:

    标签: python-3.x pandas dataframe


    【解决方案1】:

    DataFrame#drop 默认为行轴。您想删除一列。使用语法:

    df.drop(columns=[columns, input, here])
    

    您也可以通过传递参数inplace=True 来摆脱重新分配。

    【讨论】:

      猜你喜欢
      • 2019-11-15
      • 2020-04-24
      • 2022-06-16
      • 1970-01-01
      • 2021-11-16
      • 2019-08-23
      • 2018-12-06
      • 2018-04-25
      • 2017-02-01
      相关资源
      最近更新 更多