0

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:)

3
  • 2
    gdp_for_year ($) is not a valid column name. Can you post the header in master.csv? Commented Dec 5, 2019 at 20:42
  • check print(data.columns) or print(data.dtypes) to see what columns you have. Maybe you will have to add some options in read_csv() - ie. maybe file doesn't use comma separator but somthing different - ie. semicolon sep=";" Commented Dec 5, 2019 at 20:56
  • It's works for me. Please post at least the first 2 lines of your master.csv Commented Dec 5, 2019 at 21:15

2 Answers 2

1

Well, "gdp_for_year ($)" is not a valid column name

Sign up to request clarification or add additional context in comments.

1 Comment

yes. I tried to show columns and can't find the column name although it is present in the csv file!
0

Try using:

data.columns

To see what the column names are to make sure you have the matching column name

1 Comment

I tried to show columns and can't find this column name although it is present in the csv file!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.