意外 Python KeyError

Unexpected Python KeyError

我已将 CSV 文件加载到 Pandas 数据帧中:

import pandas as pd

Name     ID    Sex     M_Status    DaysOff
Joe      3      M         S           1
NaN     NaN    NaN       NaN          2
NaN     NaN    NaN       NaN          3

df = pd.read_csv('People.csv')

此数据随后将加载到 HTML 文件中。

test = """

      HTML code

 """

现在为 HTML 文件准备数据:

df1 = df.filter(['Name','ID','Sex','M_Status','DaysOff'])

file = ""

for i, rows in df1.iterrows():

   name = (df1['Name'][i])
   id = (df1['ID'][i])
   sex = (df1['Sex'][i])
   m_status = (df1['M_Status'][i])
   days_off = (df1['DaysOff'][i])

   with open(f"personInfo{i}.html", "w") as file:
      file.write(test.format(name,id,sex,m_status,days_off))
      file.close()

错误:

KeyError: 'days_off'

注意:此错误发生在 for 循环中。

谁能看出我错在哪里?当您尝试从与名称不匹配的列中获取数据时,或者如果该列没有 header 名称,则会生成此错误。然而,确实如此。

错误信息:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in 
get_loc(self, key, method, tolerance)
2656             try:
-> 2657                 return self._engine.get_loc(key)
2658             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: 'days_off'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-16-35e6b916521b> in <module>
      1     name = (df1['Name'][i])
      2     id =  (df1['ID'][i])
      3     sex = (df1['Sex'][i])
      4     m_status = (df1['M_Status'][i])
----> 5     days_off = (df1['DaysOff'][i])    

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in 
__getitem__(self, key)
   2925             if self.columns.nlevels > 1:
   2926                 return self._getitem_multilevel(key)
-> 2927             indexer = self.columns.get_loc(key)
   2928             if is_integer(indexer):
   2929                 indexer = [indexer]

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in 
get_loc(self, key, method, tolerance)
   2657                 return self._engine.get_loc(key)
   2658             except KeyError:
-> 2659                 return 
self._engine.get_loc(self._maybe_cast_indexer(key))
   2660         indexer = self.get_indexer([key], method=method, 
tolerance=tolerance)
   2661         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: 'days_off'

只是预感,但您的错误消息表明您正在尝试使用键 days_off 访问您的数据框列,而它应该是 DaysOff。我在您提供的代码中没有看到发生这种情况的任何地方,但我会仔细检查您的源代码文件以确保您使用的是正确的密钥名称。

我已经解决了这个问题,这是一个多么愚蠢的错误!

基本上header名字的末尾有一个space。

Pythonwanted/was期待什么:

days_off = (df1['DaysOff '][i])

而我给出的是:

days_off = (df1['DaysOff'][i])

非常愚蠢的人为错误。感谢所有关注它的人