当我试图打印数据集中的一列时,它显示了错误。
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(自、键、方法、容忍) 2645尝试:-> 2646返回self._engine.get_loc( key ) 2647 ( KeyError除外):
pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas._libs.hashtable.PyObjectHashTable.get_item()中的pandas_libs\hashtable_class_helper.pxi
pandas._libs.hashtable.PyObjectHashTable.get_item()中的pandas_libs\hashtable_class_helper.pxi
KeyError:“标签”
在处理上述异常的过程中,发生了另一个异常:
KeyError跟踪(最近一次调用)在->1个数据“‘Label”中
~\anaconda3\lib\site-packages\pandas\core\frame.py in getitem(self,key) 2798如果self.columns.nlevels > 1: 2799返回self._getitem_multilevel(键) -> 2800索引器=self.columns.get_loc(键) 2801 if is_integer(索引器):2802索引器=索引器
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(自、键、方法、公差) 2646返回self._engine.get_loc( key ) 2647 (KeyError除外:-> 2648返回self._engine.get_loc(self._maybe_cast_indexer(key)) 2649 )
indexer = self.get_indexer(key,method=method,tolerance=tolerance) 2650,如果indexer.ndim >1或indexer.size > 1:
pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas._libs.hashtable.PyObjectHashTable.get_item()中的pandas_libs\hashtable_class_helper.pxi
pandas._libs.hashtable.PyObjectHashTable.get_item()中的pandas_libs\hashtable_class_helper.pxi
KeyError:“标签”
data['Label']
发布于 2021-08-21 10:41:25
有可能列名有尾随空格。只需尝试打印列名&验证。
print(data.columns)
或者尝试在后面打印列。
data.columns = data.columns.str.strip()
https://stackoverflow.com/questions/68872184
复制相似问题