首页 > 解决方案 > 在 python pandas 我得到一个 keyerror

问题描述

我只有几行只是熊猫和 excel 文件的导入。我要做的就是遍历行以将 col 与某物进行比较。我还没有到达那部分,因为该行的迭代只是出现了一个关键错误。将熊猫导入为 pd

df = pd.read_excel('Warranty Detail.xlsx')

s = pd.read_excel('Book1.xlsx')

for ind, row in df.iteritems():
    print(row['Warranty'])

KeyError                                  Traceback (most recent call last)
<ipython-input-32-8c18bb0a3ba9> in <module>
      1 for ind, row in df.iteritems():
----> 2     print(row['Warranty'])
      3 

~\anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
    869         key = com.apply_if_callable(key, self)
    870         try:
--> 871             result = self.index.get_value(self, key)
    872 
    873             if not is_scalar(result):

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
   4403         k = self._convert_scalar_indexer(k, kind="getitem")
   4404         try:
-> 4405             return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
   4406         except KeyError as e1:
   4407             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type()

KeyError: 'Warranty'

标签: pythonpandas

解决方案


推荐阅读