Pandas DataFrame:set_index with inplace=True 返回 NoneType,为啥?

Posted

技术标签:

【中文标题】Pandas DataFrame:set_index with inplace=True 返回 NoneType,为啥?【英文标题】:Pandas DataFrame: set_index with inplace=True returns a NoneType, why?Pandas DataFrame:set_index with inplace=True 返回 NoneType,为什么? 【发布时间】:2017-08-08 20:07:38 【问题描述】:

如果我用“inplace=True”(在the documentation 之后)重置我的 pandas DataFrame 的索引,它会返回一个类“NoneType”。如果我用“inplace=False”重置索引,它会返回带有新索引的 DataFrame。为什么?

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 返回一个新索引:

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   

但 set_index 与 "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

【问题讨论】:

既然是inplace所以对象被修改了,为什么要返回新修改的对象呢?如果你通过了inplace=False,那么它返回新的修改对象,你检查docs了吗?很清楚:inplace : boolean, default False. Modify the DataFrame in place (do not create a new object) 为什么要返回一些东西?它进行 inplace 的修改,这意味着当前数据帧已更新。 返回对象本身很有趣,因此您可以执行df = df.&lt;do something not inplace&gt;().reset_index(inplace=True)之类的操作。 【参考方案1】:

好的,现在我明白了,感谢 cmets!

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

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

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

应该是

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

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

否则inplace index change(None)的返回值是数据框的新内容,这当然不是预期的。

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

【讨论】:

【参考方案2】:

inplace=True 始终在原始数据帧中更改。如果您想要更改 data_frame,请删除第二个参数,即 inplace = True

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

然后

print(new_data_frame)

【讨论】:

以上是关于Pandas DataFrame:set_index with inplace=True 返回 NoneType,为啥?的主要内容,如果未能解决你的问题,请参考以下文章

pandas.DataFrame.loc好慢,怎么遍历访问DataFrame比较快

将 Pandas Multiindexed DataFrame 与 Singleindexed Pandas DataFrame 合并

pandas.DataFrame.loc好慢,怎么遍历访问DataFrame比较快

python pandas dataframe 写入hdfs

pandas怎么选取dataframe中几列

详解pandas获取Dataframe元素值的几种方法