簡體   English   中英

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

[英]How To Solve KeyError: u"None of [Index([..], dtype='object')] are in the [columns]"

我正在嘗試根據我在此處的 github 中找到的內容創建 SVM model,但它一直返回此錯誤。

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

謝謝你。

我正在嘗試根據我在 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

謝謝你。

我正在嘗試根據我在 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

謝謝你。

我正在嘗試根據我在 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

謝謝你。

我在嘗試apply()結果在數據幀上創建新列時遇到了同樣的錯誤:

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

解決方案只是使用result_type="expand"參數apply()

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

在這個答案上找到了這個解決方案,值得點贊。

對此有一個后續問題。 我有一個從 XLSX 文件中讀取的數據框。 嘗試按名稱索引幾列並將其放在字符串中(源自先前的處理......)

這是 XLSX 的索引 - df.columns

Index(['Name', 'EmpID', 'Account', 'Project', 'StartDt', 'EndDt', 'Supervisor',
       'Billable ?', 'Onsite ?'],
      dtype='object')

讀取數據集時添加 index_col=0 參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM