问题描述
pandas
库中有很多选项可以更改对象的位置,例如以下语句...
In the pandas
library many times there is an option to change the object inplace such as with the following statement...
df.dropna(axis='index', how='all', inplace=True)
我很好奇,传递inplace=True
与传递inplace=False
时返回什么以及如何处理该对象.
I am curious what is being returned as well as how the object is handled when inplace=True
is passed vs. when inplace=False
.
在inplace=True
时是否所有操作都在修改self
?而当inplace=False
是立即创建的新对象(例如new_df = self
)然后返回new_df
时?
Are all operations modifying self
when inplace=True
? And when inplace=False
is a new object created immediately such as new_df = self
and then new_df
is returned?
推荐答案
通过inplace=True
时,数据将被重命名(不返回任何内容),因此您将使用:
When inplace=True
is passed, the data is renamed in place (it returns nothing), so you'd use:
df.an_operation(inplace=True)
传递inplace=False
时(这是默认值,因此不是必需的),执行操作并返回对象的副本,因此您将使用:
When inplace=False
is passed (this is the default value, so isn't necessary), performs the operation and returns a copy of the object, so you'd use:
df = df.an_operation(inplace=False)
这篇关于了解inplace = True的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!