gpt4 book ai didi

python - Pandas header 确实存在但仍然出现 KeyError

转载 作者:太空宇宙 更新时间:2023-11-04 04:04:50 27 4
gpt4 key购买 nike

当我运行我的代码时出现Keyerror 'stackoverflow'

e0 = pd.read_csv(working_dir+"E0.txt",sep=',')
e0['MTM'] = e0['stack_over_flow']

我确实输出了 e0 的列,并且在我的列中得到了 stack_over_flow

b'Super_user'
b'Personal_finance'
b'stack_over_flow'

我还尝试从 .txt 文件中手动删除 b,但仍然出现相同的错误。有人可以帮忙吗?

回溯:

Traceback (most recent call last):

File "<ipython-input-74-99e71d524b4b>", line 1, in <module>
runfile('C:/AppData/FinRecon/py_code/python3/DataJoin.py', wdir='C:/AppData/FinRecon/py_code/python3')

File "C:\Users\stack\AppData\Local\Continuum\anaconda3\anaconda3_32bit\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)

File "C:\Users\stack\AppData\Local\Continuum\anaconda3\anaconda3_32bit\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/AppData/FinRecon/py_code/python3/DataJoin.py", line 474, in <module>
M2()

File "C:/AppData/FinRecon/py_code/python3/DataJoin.py", line 41, in M2
e0['MTM'] = e0['stack_over_flow']

File "C:\Users\stack\AppData\Local\Continuum\anaconda3\anaconda3_32bit\lib\site-packages\pandas\core\frame.py", line 2927, in __getitem__
indexer = self.columns.get_loc(key)

File "C:\Users\stack\AppData\Local\Continuum\anaconda3\anaconda3_32bit\lib\site-packages\pandas\core\indexes\base.py", line 2659, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))

File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc

File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc

File "pandas/_libs/hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item

File "pandas/_libs/hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: 'stack_over_flow'

更新,我发现它是 b'' 之前和之间 每个标题。为什么将其添加到我的 .txt 文件中?

最佳答案

我更改了您的数据,如下所示假设 E0.txt 的数据如下所示。

stackoverflow,"some column name", test
1, 2, 3

使用下面的代码来检索任何列的内容。

e0 = pd.read_csv(working_dir+"E0.txt",sep=',')
e0['MTM'] = e0['stack_over_flow']

enter image description here

-- 更新--

没有 b 我创建了一个测试样本,它适用于以下输入

Super_user,Personal_finance,stack_over_flow
1, 2, 3

关于python - Pandas header 确实存在但仍然出现 KeyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57560854/

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