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)
I am curious what is being returned as well as how the object is handled when inplace=True
is passed vs. when inplace=False
.
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?
When inplace=True
is passed, the data is renamed in place (it returns nothing), so you'd use:
df.an_operation(inplace=True)
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)
So:
if inplace == False:
Assign your result to a new variable
else
No need to assign
The way I use it is
# Have to assign back to dataframe (because it is a new copy)
df = df.some_operation(inplace=False)
Or
# No need to assign back to dataframe (because it is on the same copy)
df.some_operation(inplace=True)
CONCLUSION:
if inplace is False
Assign to a new variable;
else
No need to assign
I usually use with numpy.
you use inplace=True, if you don't want to save the updated data to the same variable
data["column1"].where(data["column1"]< 5, inplace=True)
this is same as...
data["column1"] = data["column1"].where(data["column1"]< 5)
来源:https://stackoverflow.com/questions/43893457/python-pandas-understanding-inplace-true