'The column of a dataframe is not displayed

Time  GRDY_C1   GRDY_C2    
0     2.57e-02 3.05e-03
0.33  1.57e-02 4.06e-03
...

df = pd.read_csv('GRDY.csv')
print(df.columns)

Index([' Time               ', 'GRDY_C1             ', 'GRDY_C2  ',
'GRDY_C3             ', 'GRDY_C4             ', 'GRDY_C5             ',
'GRDY_C6             ', 'GRDY_F1             ', 'GRDY_F2             ',
'Unnamed: 573'],

dtype='object', length=574)

time = df["Time"]
time

This is the error:

--------------------------------------------------------------------------

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

KeyError                                  Traceback (most recent call last)
<ipython-input-68-fa9669ffa254> in <module>
----> 1 df["Time"]

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2900             if self.columns.nlevels > 1:
   2901                 return self._getitem_multilevel(key)
-> 2902             indexer = self.columns.get_loc(key)
   2903             if is_integer(indexer):
   2904                 indexer = [indexer]

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2895                 return self._engine.get_loc(casted_key)
   2896             except KeyError as err:
-> 2897                 raise KeyError(key) from err
   2898 
   2899         if tolerance is not None:

KeyError: 'Time'

This is my GRDY.csv file. First, I imported the csv file and looked the columns. I want to select "Time" column. But, I got this error. Can someone help me for this problem? Thanks.



Solution 1:[1]

As you can see, 'Time' is not part of your headers, but ' Time ' (with many surrounding spaces) is. You have to rework your column names. You can re-assign them using

df.columns=['List','Of','Headers']

And if you just want to strip the columns names use

df.columns=[e.strip() for e in list(df.columns)]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Grall