我有这样的 pandas 多索引数据框。我需要删除 bnds 等于 1.0 的行。
我尝试按照文档执行
df_f.drop('1.0', level=bnds, axis=0, inplace=True)
,但收到错误NameError:名称'bnds'未定义
Honolulu time_bnds Seattle
bnds time
1.0 2015-01-01 12:00:00 70.277412 2015-01-01 00:00:00 13.346752
2015-01-02 12:00:00 69.948593 2015-01-02 00:00:00 31.655853
2015-01-03 12:00:00 70.228027 2015-01-03 00:00:00 31.511438
2.0 2015-01-01 12:00:00 70.277412 2015-01-01 00:00:00 13.346752
2015-01-02 12:00:00 69.948593 2015-01-02 00:00:00 31.655853
更新:我按照建议使用了引号“bnds”,但出现了另一个错误
5433 def drop(
5434 self,
5435 labels: IndexLabel | None = None,
(...)
5442 errors: IgnoreRaise = "raise",
5443 ) -> DataFrame | None:
5444 """
5445 Drop specified labels from rows or columns.
5446
(...)
5579 weight 1.0 0.8
5580 """
-> 5581 return super().drop(
5582 labels=labels,
5583 axis=axis,
5584 index=index,
5585 columns=columns,
5586 level=level,
5587 inplace=inplace,
5588 errors=errors,
5589 )
File /srv/conda/envs/notebook/lib/python3.11/site-packages/pandas/core/generic.py:4788, in NDFrame.drop(self, labels, axis, index, columns, level, inplace, errors)
4786 for axis, labels in axes.items():
4787 if labels is not None:
-> 4788 obj = obj._drop_axis(labels, axis, level=level, errors=errors)
4790 if inplace:
4791 self._update_inplace(obj)
File /srv/conda/envs/notebook/lib/python3.11/site-packages/pandas/core/generic.py:4828, in NDFrame._drop_axis(self, labels, axis, level, errors, only_slice)
4826 if not isinstance(axis, MultiIndex):
4827 raise AssertionError("axis must be a MultiIndex")
-> 4828 new_axis = axis.drop(labels, level=level, errors=errors)
4829 else:
4830 new_axis = axis.drop(labels, errors=errors)
File /srv/conda/envs/notebook/lib/python3.11/site-packages/pandas/core/indexes/multi.py:2408, in MultiIndex.drop(self, codes, level, errors)
2361 """
2362 Make a new :class:`pandas.MultiIndex` with the passed list of codes deleted.
2363
(...)
2405 names=['number', 'color'])
2406 """
2407 if level is not None:
-> 2408 return self._drop_from_level(codes, level, errors)
2410 if not isinstance(codes, (np.ndarray, Index)):
2411 try:
File /srv/conda/envs/notebook/lib/python3.11/site-packages/pandas/core/indexes/multi.py:2462, in MultiIndex._drop_from_level(self, codes, level, errors)
2460 not_found = codes[values == -2]
2461 if len(not_found) != 0 and errors != "ignore":
-> 2462 raise KeyError(f"labels {not_found} not found in level")
2463 mask = ~algos.isin(self.codes[i], values)
2465 return self[mask]
KeyError: "labels ['1.0'] not found in level"
enter code here
您的问题是您将
'1.0'
作为字符串传递,而您的 bnds
列显然不是字符串类型。尝试运行以下命令:
df_f.drop(1.0, level="bnds", axis=0, inplace=True)