首页 > 解决方案 > KeyError: '1' 在处理上述异常的过程中,发生了另一个异常:

问题描述

我有一个数据框(distr),它看起来像:

Cit    A    B   C       D      F         G      H        I
(index)
01     0    5.5 18.2    33.8    50.4    68.4    91.5    101.0
02     0    7.4 21.5    40.1    57.5    76.4    94.7    101.0
03     0    7.4 21.5    40.1    57.5    76.4    94.7    101.0
04     0    7.4 21.5    40.1    57.5    76.4    94.7    101.0
05     0    7.4 21.5    40.1    57.5    76.4    94.7    101.0
06     0    7.4 21.5    40.1    57.5    76.4    94.7    101.0
07     0    7.4 21.5    40.1    57.5    76.4    94.7    101.0
08     0    7.4 21.5    40.1    57.5    76.4    94.7    101.0
09     0    7.4 21.5    40.1    57.5    76.4    94.7    101.0
10     0    7.9 24.7    44.5    62.7    82.4    97.3    101.0
11     0    7.4 21.5    40.1    57.5    76.4    94.7    101.0
12     0    7.4 21.5    40.1    57.5    76.4    94.7    101.0
13     0    7.4 21.5    40.1    57.5    76.4    94.7    101.0
14     0    7.4 21.5    40.1    57.5    76.4    94.7    101.0

我有下一个数组(cits)

array(['1', '11', '20', '22', '23', '24', '26', '28', '7'], dtype=object)

我正在尝试获取每个“Cit”列的列表

for cit in cits:
    cortes = list(distr.loc[cit])

但它引发了一个错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\AppData\Local\Continuum\anacondaf\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2896             try:
-> 2897                 return self._engine.get_loc(key)
   2898             except 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_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: '1'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-46-e13393c5908a> in <module>
      1 for cit in cits:
----> 2     cortes = list(distr.loc[cit])

~\AppData\Local\Continuum\anacondaf\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
   1422 
   1423             maybe_callable = com.apply_if_callable(key, self.obj)
-> 1424             return self._getitem_axis(maybe_callable, axis=axis)
   1425 
   1426     def _is_scalar_access(self, key: Tuple):

~\AppData\Local\Continuum\anacondaf\lib\site-packages\pandas\core\indexing.py in _getitem_axis(self, key, axis)
   1848         # fall thru to straight lookup
   1849         self._validate_key(key, axis)
-> 1850         return self._get_label(key, axis=axis)
   1851 
   1852 

~\AppData\Local\Continuum\anacondaf\lib\site-packages\pandas\core\indexing.py in _get_label(self, label, axis)
    158             raise IndexingError("no slices here, handle elsewhere")
    159 
--> 160         return self.obj._xs(label, axis=axis)
    161 
    162     def _get_loc(self, key: int, axis: int):

~\AppData\Local\Continuum\anacondaf\lib\site-packages\pandas\core\generic.py in xs(self, key, axis, level, drop_level)
   3735             loc, new_index = self.index.get_loc_level(key, drop_level=drop_level)
   3736         else:
-> 3737             loc = self.index.get_loc(key)
   3738 
   3739             if isinstance(loc, np.ndarray):

~\AppData\Local\Continuum\anacondaf\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2897                 return self._engine.get_loc(key)
   2898             except KeyError:
-> 2899                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2900         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2901         if indexer.ndim > 1 or 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_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: '1'

你知道如何解决它或无论如何避免它吗?

我一直在搜索,我认为问题出在 loc 函数上,

但我不知道如何解决它或为什么会出现这些错误。

标签: pythonpandaslistdataframe

解决方案


您可以使用.to_list()

for col in df:
  print(df[col].to_list())

推荐阅读