python keyerror 调用栈,python - 意外的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循环内。

谁能看到我要去哪里错了? 当您尝试从与名称不匹配的列中获取数据时,或者如果该列不具有该标题名称,则会生成此错误。 但是,确实如此。

错误信息:

---------------------------------------------------------------------------

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)

in

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'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值