读取python中的csv列返回

2024-10-17 08:20:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个关于某个csv列的问题。 当试图阅读以下列时:

import pandas as pd

data = pd.read_csv('master.csv')
print(data['gdp_for_year ($)'])

它给出以下错误:

Traceback (most recent call last):
  File "C:\Users\work\venv\Suicide rate prediction based on GDB\lib\site-packages\pandas\core\indexes\base.py", line 2897, in get_loc
    return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'gdp_for_year ($)'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/work/PycharmProjects/Suicide rate prediction based on GDB/project.py", line 4, in <module>
    print(data['gdp_for_year ($)'])
  File "C:\Users\work\venv\Suicide rate prediction based on GDB\lib\site-packages\pandas\core\frame.py", line 2995, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:\Users\work\venv\Suicide rate prediction based on GDB\lib\site-packages\pandas\core\indexes\base.py", line 2899, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/_libs/index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'gdp_for_year ($)'

Process finished with exit code 1

有人能看出哪里不对劲吗? 提前感谢:)


Tags: inpandasforgetindexlineyearusers