Search code examples
pythonpandascsvjupyter-notebookkeyerror

KeyError when using pd.read_csv in pandas


I am trying to read my csv file but KeyError occurs I can say for sure that column is exactly called 'TG', but for some unknow reason pandas can't read it How can I fix that?

import pandas as pd
import warnings 
warnings.filterwarnings('ignore')
stars = pd.read_csv('C:\excel\pulsar_stars_new.csv', on_bad_lines='skip')
stars_train = stars[((stars['TG'] == 0) & (stars['MIP'] >= 94.6640625) & (stars['MIP'] <= 95.2890625))]


KeyError                                  Traceback (most recent call last)
File C:\anaconda\lib\site-packages\pandas\core\indexes\base.py:3802, in Index.get_loc(self, key,    method, tolerance)
3801 try:
-> 3802     return self._engine.get_loc(casted_key)
3803 except KeyError as err:

File C:\anaconda\lib\site-packages\pandas\_libs\index.pyx:138, in    pandas._libs.index.IndexEngine.get_loc()

File C:\anaconda\lib\site-packages\pandas\_libs\index.pyx:165, in  pandas._libs.index.IndexEngine.get_loc()

File pandas\_libs\hashtable_class_helper.pxi:5745, in  pandas._libs.hashtable.PyObjectHashTable.get_item()

File pandas\_libs\hashtable_class_helper.pxi:5753, in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'TG'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
Cell In[30], line 1
----> 1 stars_train = stars[((stars['TG'] == 0) & (stars['MIP'] >= 94.6640625) & (stars['MIP'] <=   95.2890625))]

File C:\anaconda\lib\site-packages\pandas\core\frame.py:3807, in DataFrame.__getitem__(self, key)
3805 if self.columns.nlevels > 1:
3806     return self._getitem_multilevel(key)
-> 3807 indexer = self.columns.get_loc(key)
3808 if is_integer(indexer):
3809     indexer = [indexer]

File C:\anaconda\lib\site-packages\pandas\core\indexes\base.py:3804, in 
Index.get_loc(self, key, method, tolerance)
3802     return self._engine.get_loc(casted_key)
3803 except KeyError as err:
-> 3804     raise KeyError(key) from err
3805 except TypeError:
3806     # If we have a listlike key, _check_indexing_error will raise
3807     #  InvalidIndexError. Otherwise we fall through and re-raise
3808     #  the TypeError.
3809     self._check_indexing_error(key)

KeyError: 'TG'

Help me please, how to make pandas read it?


Solution

  • stars[((stars['TG'] == 0)

    This has the correct syntax.

    The stack trace shows that the column does not exist in your dataframe.

    I can say for sure that column is exactly called 'TG'

    Most likely, your dataframe is not read correctly by read_csv. read_csv has over 50 parameters. You have to make sure that your resulting dataframe (stars) looks like a dataframe with columns and rows.

    The second possibility, you are wrong about TG.