I have a problem concerning a certain csv column. When trying to read this column as following:
import pandas as pd
data = pd.read_csv('master.csv')
print(data['gdp_for_year ($)'])
It gives the following error:
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
Can anyone tell what's wrong? Thanks in advance:)
gdp_for_year ($)is not a valid column name. Can you post the header inmaster.csv?print(data.columns)orprint(data.dtypes)to see what columns you have. Maybe you will have to add some options inread_csv()- ie. maybe file doesn't use comma separator but somthing different - ie. semicolonsep=";"master.csv