pandas 数据框:set_index和inplace = True会返回NoneType,为什么? [英] Pandas dataframe: set_index with inplace=True returns a NoneType, why?
问题描述
If I reset the index of my Pandas dataframe with "inplace=True" (following the documentation) it returns a class 'NoneType'. If I reset the index with "inplace=False" it returns the dataframe with the new index. Why?
print(type(testDataframe))
print(testDataframe.head())
返回:
<class 'pandas.core.frame.DataFrame'>
ALandbouwBosbouwEnVisserij AantalInkomensontvangers AantalInwoners \
0 73780.0 None 16979120
1 290.0 None 25243
2 20.0 None 3555
Set_index返回一个新索引:
Set_index returns a new index:
testDataframe = testDataframe.set_index(['Codering'])
print(type(testDataframe))
print(testDataframe.head())
返回
<class 'pandas.core.frame.DataFrame'>
ALandbouwBosbouwEnVisserij AantalInkomensontvangers \
Codering
NL00 73780.0 None
GM1680 290.0 None
WK168000 20.0 None
BU16800000 15.0 None
但是与"inplace = True"相同的set_index:
But the same set_index with "inplace=True":
testDataframe = testDataframe.set_index(['Codering'], inplace=True)
print(type(testDataframe))
print(testDataframe.head())
返回
<class 'NoneType'>
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-50-0d6304ebaae1> in <module>()
版本信息:
python: 3.4.4.final.0
python-bits: 64
pandas: 0.18.1
numpy: 1.11.1
IPython: 5.2.2
推荐答案
好的,现在我明白了,谢谢您的评论!
Ok, now I understand, thanks for the comments!
因此inplace = True应该返回None ,并在原始对象中进行更改.似乎在再次列出数据框时,没有任何更改.
So inplace=True should return None and make the change in the original object. It seemed that on listing the dataframe again, no changes were present.
但是我当然不应该将返回值分配给数据框,即
testDataframe = testDataframe.set_index(['Codering'], inplace=True)
应该是
testDataframe.set_index(['Codering'], inplace=True)
或
testDataframe = testDataframe.set_index(['Codering'], inplace=False)
否则,就地索引更改的返回值(无)是数据帧的新内容,这当然不是预期的.
otherwise the return value of the inplace index change (None) is the new content of the dataframe which is of course not the intend.
我敢肯定,这对许多人来说都是显而易见的,现在对我也是如此,但这并非没有你的帮助,谢谢!!!
I am sure this is obvious to many and now it is to me as well but it wasn't without your help, thanks!!!
这篇关于 pandas 数据框:set_index和inplace = True会返回NoneType,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!