Pandas 数据框:set_index with inplace=True 返回 NoneType,为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42854598/
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 03:13:01  来源:igfitidea点击:

Pandas dataframe: set_index with inplace=True returns a NoneType, why?

python-3.xpandas

提问by Wouter

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?

如果我使用“inplace=True”(按照文档)重置我的 Pandas 数据框的索引,它会返回一个类“NoneType”。如果我用“inplace=False”重置索引,它会返回带有新索引的数据帧。为什么?

print(type(testDataframe))
print(testDataframe.head())

returns:

返回:

<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 returns a new index:

Set_index 返回一个新索引:

testDataframe = testDataframe.set_index(['Codering'])
    print(type(testDataframe))
    print(testDataframe.head())

returns

回报

<class 'pandas.core.frame.DataFrame'>
            ALandbouwBosbouwEnVisserij AantalInkomensontvangers  \
Codering                                                          
NL00                           73780.0                     None   
GM1680                           290.0                     None   
WK168000                          20.0                     None   
BU16800000                        15.0                     None   

But the same set_index with "inplace=True":

但同样的 set_index 与“inplace=True”:

testDataframe = testDataframe.set_index(['Codering'], inplace=True)
print(type(testDataframe))
print(testDataframe.head())

returns

回报

<class 'NoneType'>
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-50-0d6304ebaae1> in <module>()

Version info:

版本信息:

python: 3.4.4.final.0
python-bits: 64
pandas: 0.18.1
numpy: 1.11.1
IPython: 5.2.2

回答by Wouter

Ok, now I understand, thanks for the comments!

好的,现在我明白了,感谢您的评论!

So inplace=True should return None andmake the change in the original object. It seemed that on listing the dataframe again, no changes were present.

所以 inplace=True 应该返回 None在原始对象中进行更改。似乎在再次列出数据框时,没有任何变化。

But of course I should not have assigned the return valueto the dataframe, i.e.

但是当然我不应该将返回值分配给数据框,即

testDataframe = testDataframe.set_index(['Codering'], inplace=True)

should just be

应该只是

testDataframe.set_index(['Codering'], inplace=True)

or

或者

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!!!

我相信这对很多人来说都是显而易见的,现在对我来说也是如此,但这并非没有你的帮助,谢谢!!!

回答by Shubham gupta

inplace=True is always changed in the original data_frame. If you want a changed data_frame then remove second parameter i.e inplace = True

inplace=True 始终在原始数据帧中更改。如果你想要一个改变的 data_frame 然后删除第二个参数即inplace = True

new_data_frame = testDataframe.set_index(['Codering'])

Then

然后

print(new_data_frame)