gpt4 book ai didi

python - 意外的 Python KeyError

转载 作者:行者123 更新时间:2023-12-01 06:59:50 25 4
gpt4 key购买 nike

我已将 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)
<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。我在您提供的代码中没有看到发生这种情况的任何地方,但我会仔细检查您的源代码文件以确保您使用正确的 key 名称。

关于python - 意外的 Python KeyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58684526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com