Search code examples
pythonpandasdataframesortingkeyerror

Can't pick a dataframe column


I'm trying to pick a column from a dataframe using sort_values() function but it isn't working as I desired. Let me show you in code.

identity = pd.read_csv(r"demographic_info.csv", skipinitialspace=True)
eeg = pd.read_csv(r"EEG_data.csv", skipinitialspace=True)

identity.columns

identity = identity.rename(columns={' age': 'age',
                        ' ethnicity': 'ethnicity' ,
                       ' gender': 'gender'})

identity.sort_values(by = 'gender', inplace=True, axis = 1)

I saw hidden white spaces in columns and I fixed the problem in two ways. Although it gives the same error in every way:

Traceback (most recent call last):

  File "<ipython-input-8-f8783f09d99b>", line 15, in <module>
    identity.sort_values(by = 'gender', inplace=True, axis = 1)

  File "C:\Users\user\Anaconda3\lib\site-packages\pandas\core\frame.py", line 4993, in 
    sort_values
    k = self._get_label_or_level_values(by, axis=axis)

  File "C:\Users\user\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1774, in 
    _get_label_or_level_values
    raise KeyError(key)

KeyError: 'gender'

Solution

  • You should sort with axis=0, not axis=1:

    identity.sort_values(by = 'gender', inplace=True, axis = 0)