pandas 如何解决KeyError:u“[Index([..],dtype='object')]都不在[columns]中”

eaf3rand  于 2023-11-15  发布在  其他
关注(0)|答案(5)|浏览(150)

我试图从我在github here中找到的东西创建一个SVM模型,但它总是返回这个错误。

Traceback (most recent call last):
  File "C:\Users\Me\Documents\#e\projects\Sign-Language-Glove-master\modeling.py", line 22, in <module>
    train_features = train[['F1','F2','F3','F4','F5','X','Y','Z','C1','C2']]
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2934, in __getitem__
    raise_missing=True)
  File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 1354, in _convert_to_indexer
    return self._get_listlike_indexer(obj, axis, **kwargs)[1]
  File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 1161, in _get_listlike_indexer
    raise_missing=raise_missing)
  File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 1246, in _validate_read_indexer
    key=key, axis=self.obj._get_axis_name(axis)))
KeyError: u"None of [Index([u'F1', u'F2', u'F3', u'F4', u'F5', u'X', u'Y', u'Z', u'C1', u'C2'], dtype='object')] are in the [columns]"

字符串
这是我的代码。

import pandas as pd
dataframe= pd.read_csv("lettera.csv", delimiter=',')
df=pd.DataFrame(dataframe)

from sklearn.model_selection import train_test_split
train, test = train_test_split(df, test_size = 0.2)

train_features = train[['F1','F2','F3','F4','F5','X','Y','Z','C1','C2']]


这些是csv文件的内容。

LABEL, F1, F2, F3, F4, F5, X, Y, Z, C1, C2

1, 631, 761, 739, 751, 743, 14120, -5320, 7404, 0, 0

1, 632, 759, 740, 751, 744, 14108, -5276, 7444, 0, 0

1, 630, 761, 740, 752, 743, 14228, -5104, 7680, 0, 0

1, 630, 761, 738, 750, 743, 14256, -5148, 7672, 0, 0

1, 632, 759, 740, 751, 744, 14172, -5256, 7376, 0, 0

1, 632, 759, 742, 751, 746, 14288, -5512, 7412, 0, 0

1, 632, 759, 742, 751, 744, 14188, -5200, 7416, 0, 0

1, 634, 759, 738, 751, 743, 14252, -5096, 7524, 0, 0

1, 630, 759, 739, 751, 743, 14364, -5124, 7612, 0, 0

1, 630, 759, 740, 751, 744, 14192, -5316, 7424, 0, 0

1, 631, 760, 739, 752, 743, 14292, -5100, 7404, 0, 0

1, 634, 759, 738, 751, 742, 14232, -5188, 7468, 0, 0

1, 632, 759, 740, 751, 744, 14288, -5416, 7552, 0, 0

1, 630, 760, 739, 752, 743, 14344, -5072, 7816, 0, 0

1, 631, 760, 739, 752, 743, 14320, -4992, 7444, 0, 0

1, 630, 762, 739, 751, 746, 14220, -5172, 7544, 0, 0

1, 630, 759, 739, 751, 742, 14280, -5176, 7416, 0, 0

1, 630, 760, 738, 752, 740, 14360, -5028, 7468, 0, 0

1, 632, 759, 738, 752, 741, 14384, -5108, 7364, 0, 0

1, 629, 757, 737, 751, 741, 14224, -5108, 7536, 0, 0

1, 629, 758, 740, 751, 744, 14412, -5136, 7956, 0, 0

1, 629, 761, 740, 750, 744, 14468, -4868, 7100, 0, 0

1, 629, 760, 738, 752, 741, 14504, -4964, 6600, 0, 0

1, 629, 758, 738, 749, 741, 14440, -5112, 6828, 0, 0

1, 629, 760, 738, 752, 741, 14484, -5016, 7556, 0, 0

gopyfrb3

gopyfrb31#

问题是列名中有 * 空格 *;下面是当我保存您的数据并像您所做的那样加载数据框时得到的结果:

df.columns
# result:
Index(['LABEL', ' F1', ' F2', ' F3', ' F4', ' F5', ' X', ' Y', ' Z', ' C1',
       ' C2'],
      dtype='object')

字符串
因此,在列名中放回这些空格可以消 debugging 误:

train_features = train[[' F1',' F2',' F3',' F4',' F5',' X',' Y',' Z',' C1',' C2']] # works OK


但可以说,在列名中使用空格并不是一个好的做法(你看到了会发生什么!);所以最好在加载过程中消除它们。下面是端到端的代码(也消除了不必要的第二个字符串):

import pandas as pd
df= pd.read_csv("lettera.csv", delimiter=',', header=None, skiprows=1, names=['LABEL','F1','F2','F3','F4','F5','X','Y','Z','C1','C2'])

from sklearn.model_selection import train_test_split
train, test = train_test_split(df, test_size = 0.2)
train_features = train[['F1','F2','F3','F4','F5','X','Y','Z','C1','C2']] # works OK

h6my8fg2

h6my8fg22#

在我的例子中,这是因为我的数组中的列名有空格,我在我的df中重命名了列名,用_代替了空格。

# remove special character
df.columns = df.columns.str.replace(' ', '')

字符串

uurity8g

uurity8g3#

我在尝试从apply()结果中创建新列时也遇到了同样的错误: 

>>> df[["foo","bar"]] = df.apply(lambda r: ["foobar","baz"], axis=1)
"None of [Index(['foo', 'bar'], dtype='object')] are in the [columns]"

字符串
解决方案是简单地使用apply()result_type="expand"参数: 

df[["foo","bar"]] = df.apply(lambda r: ["foobar","baz"], axis=1, result_type="expand")


我发现这个解决方案on this answer,值得推荐。

twh00eeo

twh00eeo4#

尝试与

import pandas as pd
dataframe= pd.read_csv("lettera.csv", delimiter=','sep=r', ')

字符串
我在", ;, ", -上遇到了同样的问题,我在你的数据中看到了这个(", ")
如果您可以在数据集中看到单独的符号,则可以使用sep =

anauzrmj

anauzrmj5#

在阅读数据集时添加index_col=0参数

相关问题