无法选择数据框列
Can't pick a dataframe column
我正在尝试使用 sort_values() 函数从数据框中选择一列,但它没有按我的预期工作。让我用代码告诉你。
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)
我在列中看到隐藏的空格,我用两种方法解决了这个问题。尽管它在各个方面都给出了相同的错误:
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'
您应该使用 axis=0 而不是 axis=1 进行排序:
identity.sort_values(by = 'gender', inplace=True, axis = 0)
我正在尝试使用 sort_values() 函数从数据框中选择一列,但它没有按我的预期工作。让我用代码告诉你。
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)
我在列中看到隐藏的空格,我用两种方法解决了这个问题。尽管它在各个方面都给出了相同的错误:
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'
您应该使用 axis=0 而不是 axis=1 进行排序:
identity.sort_values(by = 'gender', inplace=True, axis = 0)