【问题标题】:KeyError: "['cut'] not found in axis"KeyError:“在轴中找不到 ['cut']”
【发布时间】:2020-04-24 06:09:23
【问题描述】:

我正在处理数千行数据,试图缩小对某些谷物的搜索范围。为此,我有一个包含大约 20 个不同值的“资产”列,其中我需要接收相邻列“负载”中所有行的总和。

我想从我的数据集中删除不必要的行。首先,我将所有额外资产重新标记为“剪切”(如下例所示),以便我可以管理一个 .drop 命令。以下是它的编码方式:

df14['Asset'] = df14["Asset"].str.replace('BEANS', 'cut')
df14.drop("cut", axis=0)
set(df14['Asset'])

这是我收到的错误:

KeyError                                  Traceback (most recent call last)
<ipython-input-593-40006512df80> in <module>
----> 1 df14.drop("cut", axis=0)
      2 set(df14['Asset'])

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in drop(self, labels, axis, index, columns, level, inplace, errors)
   4100             level=level,
   4101             inplace=inplace,
-> 4102             errors=errors,
   4103         )
   4104 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py in drop(self, labels, axis, index, columns, level, inplace, errors)
   3912         for axis, labels in axes.items():
   3913             if labels is not None:
-> 3914                 obj = obj._drop_axis(labels, axis, level=level, errors=errors)
   3915 
   3916         if inplace:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py in _drop_axis(self, labels, axis, level, errors)
   3944                 new_axis = axis.drop(labels, level=level, errors=errors)
   3945             else:
-> 3946                 new_axis = axis.drop(labels, errors=errors)
   3947             result = self.reindex(**{axis_name: new_axis})
   3948 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in drop(self, labels, errors)
   5338         if mask.any():
   5339             if errors != "ignore":
-> 5340                 raise KeyError("{} not found in axis".format(labels[mask]))
   5341             indexer = indexer[~mask]
   5342         return self.delete(indexer)

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

我尝试了几个命令来删除这些行,例如:

df14.drop(["cut"], inplace = True) 

df14[~df14['Asset'].isin(to_drop)]
df14[df14['Asset'].str.contains('cut', na = True)]

而且它们都结出相同的果实。

当我编码时

df14 = df14[~df14["Asset"].str.contains('BEANS')]

它不会从我的最终计算中删除下一列的负载数。

是否可以删除带有特定标签的所有数据行,以便我可以将 20 个资产修剪为 7 个资产?

谢谢

【问题讨论】:

    标签: python-3.x pandas csv dataframe jupyter-notebook


    【解决方案1】:

    pd.drop 按列或行工作。你给列名来删除一列或索引来删除一行。 Andaxis=0 表示 index-wise。由于您没有名为“cut”的索引,因此会出现错误。

    我建议这样做:

    df = df.loc[df['Asset'] != 'cut']
    

    【讨论】:

      【解决方案2】:

      我认为 df14.drop("cut", axis=0) 失败了,因为它正在 df14 的索引中寻找值“cut”。您可以将资产列指定为索引,请参阅关于 drop 的 pandas 文档以了解如何,但我认为更好的解决方案可能是

      df14 = df14.query('asset != "cut"') 
      

      我不能说我知道这是否是最快的解决方案,因为我通常使用小型数据集,我不必过多担心性能。

      【讨论】:

        【解决方案3】:

        这应该可以完成工作。 在这里,您基本上选择了除 'cut' 以外的所有行

        df14 = df14.loc[df14['Asset'] != 'cut']

        【讨论】:

          猜你喜欢
          • 2019-11-15
          • 2019-12-17
          • 2022-06-16
          • 2018-12-06
          • 2018-04-25
          • 2017-02-01
          • 2018-11-10
          • 2017-05-21
          • 2013-10-17
          相关资源
          最近更新 更多