What does inplace mean in Pandas?
Last Updated :
01 Jun, 2022
In this article, we will see Inplace in pandas. Inplace is an argument used in different functions. Some functions in which inplace is used as an attributes like, set_index(), dropna(), fillna(), reset_index(), drop(), replace() and many more. The default value of this attribute is False and it returns the copy of the object.
Here we are using fillna() methods.
Syntax: dataframe.fillna(dataframe.mean(), inplace = False)
Let’s understand this method with step-wise implementation:
Step 1. First, we import all the required libraries.
Step 2.Creating dataframe.
Python3
dataframe = pd.DataFrame({ 'Name' :[ 'Shobhit' , 'vaibhav' ,
'vimal' , 'Sourabh' ],
'Class' :[ 11 , 12 , 10 , 9 ],
'Age' :[ 18 , 20 , 21 , 17 ]})
display(dataframe)
|
Output :
Step 3.To see the inplace use we are going to use the rename function where we are renaming “Name” Column to “FirstName”.
In this step, We will not use inplace in our code.
Python3
new_data = dataframe.rename(columns = { 'Name' : 'FirstName' })
display(new_data)
|
Output :
We can clearly see that there are no changes in the original dataframe. Through this, we conclude that the default value of inplace is False.
Now in this step, we will use inplace with False value.
Python3
new_data_2 = dataframe.rename(columns = { 'Name' : 'FirstName' },
inplace = False )
display(new_data_2)
|
Output :
Again we can clearly see that there are no changes in the original dataset.
At last, we are putting inplace value equal to True.
Python3
dataframe.rename(columns = { 'Name' : 'FirstName' },
inplace = True )
print (dataframe)
|
Output :
Finally, we can see that the original dataframe columns have been modified from “Name” to “FirstName”.
Below is the complete program based on the above approach :
Python3
import pandas as pd
dataframe = pd.DataFrame({ 'Name' :[ 'Shobhit' , 'Vaibhav' ,
'Vimal' , 'Sourabh' ],
'Class' :[ 11 , 12 , 10 , 9 ],
'Age' :[ 18 , 20 , 21 , 17 ]})
display(dataframe)
new_data = dataframe.rename(columns = { 'Name' : 'FirstName' })
display(new_data)
display(dataframe)
new_data_2 = dataframe.rename(columns = { 'Name' : 'FirstName' },
inplace = False )
display(new_data_2)
display(dataframe)
dataframe.rename(columns = { 'Name' : 'FirstName' },
inplace = True )
display(dataframe)
|
Output :
Share your thoughts in the comments
Please Login to comment...