--------------------------------------------------------------------------- KeyError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_12396\356241790.py in <module> 3 if (df['应发库'][i]!="sz"and df['应发库'][i]!="cs"and df['应发库'][i]!="sy"and df['应发库'][i]!="sh"and df['应发库'][i]!="cd"and df['应发库'][i]!="xa"and df['应发库'][i]!="km"and df['应发库'][i]!="jn"and df['应发库'][i]!="bj"): 4 droplist.append(i) ----> 5 df2=df1.drop(labels=droplist,axis=0) ~\AppData\Local\anaconda3\envs\tensorflow\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs) 309 stacklevel=stacklevel, 310 ) --> 311 return func(*args, **kwargs) 312 313 return wrapper ~\AppData\Local\anaconda3\envs\tensorflow\lib\site-packages\pandas\core\frame.py in drop(self, labels, axis, index, columns, level, inplace, errors) 4911 level=level, 4912 inplace=inplace, -> 4913 errors=errors, 4914 ) 4915 ~\AppData\Local\anaconda3\envs\tensorflow\lib\site-packages\pandas\core\generic.py in drop(self, labels, axis, index, columns, level, inplace, errors) 4148 for axis, labels in axes.items(): 4149 if labels is not None: -> 4150 obj = obj._drop_axis(labels, axis, level=level, errors=errors) 4151 4152 if inplace: ~\AppData\Local\anaconda3\envs\tensorflow\lib\site-packages\pandas\core\generic.py in _drop_axis(self, labels, axis, level, errors) 4183 new_axis = axis.drop(labels, level=level, errors=errors) 4184 else: -> 4185 new_axis = axis.drop(labels, errors=errors) 4186 result = self.reindex(**{axis_name: new_axis}) 4187 ~\AppData\Local\anaconda3\envs\tensorflow\lib\site-packages\pandas\core\indexes\base.py in drop(self, labels, errors) 6015 if mask.any(): 6016 if errors != "ignore": -> 6017 raise KeyError(f"{labels[mask]} not found in axis") 6018 indexer = indexer[~mask] 6019 return self.delete(indexer) KeyError: '[357143] not found in axis'
时间: 2024-03-03 13:51:18 浏览: 67
这个错误是由于你试图删除一个不存在的索引标签引起的。你需要检查代码,确保你要删除的标签是存在的,并且没有重复的标签。如果你不确定要删除的标签是否存在,你可以使用 Pandas 的 `isin()` 函数来检查。例如,你可以使用以下代码来检查要删除的标签是否存在于数据框中:
```
print(set(droplist).issubset(set(df1.index)))
```
这将返回一个布尔值,告诉你要删除的标签是否都存在于数据框中。如果不是,则需要进一步检查要删除的标签是否正确。
阅读全文