What does the argument "inplace" do in Pandas' DataFrame.dropna()?

https://app.dataquest.io/m/152/conditional-plots/2/introduction-to-the-data-set
Hi, I cannot understand what inplace does in dataframe.drop(inplace).
what would happen if I make it True?
could somebody explain it ?

Thank you very much in advance!

Please note that I have modified your Question Title so that it is more descriptive of your question. For future questions, try to use more descriptive titles so that they are easily discoverable by other students.

I am also assuming you meant dropna and not drop given your linked Mission. The working of inplace, however, does not change regardless of the Pandas method used.

But consider a simplified example -

a = [1, 2, 3, NaN, 5]

The above is a list only for the purpose of this simple example. But consider that a is your DataFrame.

If you do

a = a.dropna()

Then you will drop the rows in a that contain missing values, and save the result back to a.

So, print(a) would give us [1, 2, 3, 5].

You could also do

b = a.dropna()

In the above, the results of a.dropna() are saved into b. So, print(b) would give us [1, 2, 3, 5]. However, if you print(a), you will see that it’s still [1, 2, 3, NaN, 5].

We could also do -

a.dropna()

Same operation above, however, the result from the above is not being saved back into a variable. Therefore, if we did print(a), we would get the original values - [1, 2, 3, NaN, 5].

Now, what if we wanted to drop the missing values, wanted to make sure that a gets updated without having to assign the result back into a variable?

That’s what inplace does. It carries out the operation in-place. So,

a.dropna(inplace=True)

would result in the operation, and if we did print(a), we would get - [1, 2, 3, 5]. As you can see, a was update in-place.

The above is not really that different from the following (which we saw above as well) -

a = a.dropna()

So, why is it used then if we can do the above directly?

Well, that seems to be a matter of some “debate”. The scope of that discussion is something I would recommend not focusing on, but you can check out the following two resources if you’d like to -

Note - As already mentioned above, the example of a was for demonstration purposes only.

2 Likes