Understanding Inplace=True in Pandas

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



Leave a reply



Submit