Understanding inplace=True in pandas
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)
Pandas manipulating a DataFrame inplace vs not inplace (inplace=True vs False)
In general, there is no difference between inplace=True
and returning an explicit copy - in both cases, a copy is created. It just so happens that, in the first case, the data in the copy is copied back into the original df
object, so reassignment is not necessary.
Furthermore, note that as of v0.21
, df.sort
is deprecated, use sort_values
instead.
Pandas inplace meaning
If you pass the parameter inplace=False
, it will create a new DataFrame
on which the operation has been performed.
If you pass the parameter inplace=True
, it will apply the operation directly on the DataFrame
you're working on. Hence, the following lines are doing the same thing (conceptually):
df.replace('?',-99999, inplace=True)
df = df.replace('?', -99999, inplace=False)
Using the inplace
version allow you to work on a single DataFrame
. Using the other version allows you to create a new DataFrame
on which you can work while keeping the original one, like this:
df_dropped = df.replace('?', -99999, inplace=False)
Related Topics
Which Python Memory Profiler Is Recommended
How to Uninstall Python 2.7 on a MAC Os X 10.6.4
Why Does Integer Division Yield a Float Instead of Another Integer
How to Prettyprint a Json File
Adding a New Pandas Column With Mapped Value from a Dictionary
Django Csrf Check Failing With an Ajax Post Request
How to Search For a String in Text Files
Pretty-Print an Entire Pandas Series/Dataframe
How to Get the Path and Name of the File That Is Currently Executing
How to Use Glob() to Find Files Recursively
Find the Current Directory and File'S Directory
How Can the Euclidean Distance Be Calculated With Numpy
Spawning Multiple Instances of the Same Object Concurrently in Python
Are List-Comprehensions and Functional Functions Faster Than "For Loops"