类型错误:无法使用这些索引器对 <class 'pandas.indexes.base.Index'> 进行标签索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45452096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:09:24  来源:igfitidea点击:

TypeError: cannot do label indexing on <class 'pandas.indexes.base.Index'> with these indexers

pythonpandasdataframetypeerrorrenaming

提问by Lazloo Xp

I face an error after renaming a pandas dataframe column:

重命名 Pandas 数据框列后出现错误:

B=pd.DataFrame(data=[[1,1,1],[2,2,2]],columns={'A','B','C'})
print(B.loc[0,'B'])
B = B.rename(index=str,columns={'B':'B_B'})
print(B.loc[0,'B_B'])

This code leads to folowing output:

此代码导致以下输出:

> 1

>Traceback (most recent call last):
  File "C:\Users\FreieL01\AppData\Local\Continuum\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-29-6df9197203b9>", line 4, in <module>
    print(B.loc[0,'B_B'])
  File "C:\Users\FreieL01\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1310, in __getitem__
    return self._getitem_tuple(key)
  File "C:\Users\FreieL01\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 796, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "C:\Users\FreieL01\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 922, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "C:\Users\FreieL01\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1482, in _getitem_axis
    self._has_valid_type(key, axis)
  File "C:\Users\FreieL01\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1409, in _has_valid_type
    key = self._convert_scalar_indexer(key, axis)
  File "C:\Users\FreieL01\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 196, in _convert_scalar_indexer
    return ax._convert_scalar_indexer(key, kind=self.name)
  File "C:\Users\FreieL01\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\indexes\base.py", line 1171, in _convert_scalar_indexer
    return self._invalid_indexer('label', key)
  File "C:\Users\FreieL01\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\indexes\base.py", line 1284, in _invalid_indexer
    kind=type(key)))
TypeError: cannot do label indexing on <class 'pandas.indexes.base.Index'> with these indexers [0] of <class 'int'>}

It seems that the renaming changes the type of my datafframe object ...

重命名似乎改变了我的 datafframe 对象的类型......

采纳答案by cs95

Running df.rename(index=str, ...)causes your dataframe indices to be typecast to string types, so they'd be strings. You can confirm this by printing them out.

运行df.rename(index=str, ...)会导致您的数据帧索引被类型转换为字符串类型,因此它们将是字符串。您可以通过打印出来确认这一点。

In [19]: B.index
Out[19]: Index(['0', '1'], dtype='object')

Consequently, this works:

因此,这有效:

In [18]: print(B.loc['0', 'B_B'])
1

If you don't want this, just don't pass an indexattribute when renaming your Bcolumn, like this:

如果您不想要这样,请index在重命名B列时不要传递属性,如下所示:

B = B.rename(columns={'B':'B_B'})