Skip to content
Related Articles

Related Articles

What does inplace mean in Pandas?
  • Last Updated : 11 Dec, 2020

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(), inplcae = False) 
 

Let’s understand this method with step-wise implementation:

Step 1. First, we import all the required libraries.



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import required module
import pandas as pd

chevron_right


Step 2.Creating dataframe.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# creating dataframe
dataframe = pd.DataFrame({'Name':['Shobhit','vaibhav',
                                'vimal','Sourabh'],
                          
                        'Class':[11,12,10,9],
                        'Age':[18,20,21,17]})
  
# Checking created dataframe
display(dataframe)

chevron_right


  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



filter_none

edit
close

play_arrow

link
brightness_4
code

# without using inplace renaming the column
new_data = dataframe.rename(columns = {'Name':'FirstName'})
  
# check new_data
display(new_data)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# putting inplace=False
new_data_2 = dataframe.rename(columns = {'Name':'FirstName'},
                            inplace = False)
  
#check new_data_2
display(new_data_2)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Putting Inplace=True
dataframe.rename(columns = {'Name':'FirstName'},
                 inplace = True)
   
# check whether dataframe is modidfied or not
print(dataframe)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas
import pandas as pd
  
# creating dataframe
dataframe=pd.DataFrame({'Name':['Shobhit','Vaibhav',
                                'Vimal','Sourabh'],
                          
                        'Class':[11,12,10,9],
                        'Age':[18,20,21,17]})
  
# Checking created dataframe
# copied dataframe
display(dataframe)
  
# without using inplace renaming the column
new_data = dataframe.rename(columns = {'Name':'FirstName'})
  
# Copied dataframe
display(new_data)  
  
# checking whether dataframe is modified or not
# Original dataframe
display(dataframe)
  
# putting inplace=False
new_data_2 = dataframe.rename(columns = {'Name':'FirstName'},
                              inplace = False)
  
# Copied dataframe
display(new_data_2)
  
# checking whether dataframe is modified or not
# Original dataframe
display(dataframe)
  
# Putting Inplace=True
dataframe.rename(columns = {'Name':'FirstName'},
                 inplace = True)
  
# checking whether dataframe is modified or not
# Original dataframe
display(dataframe)

chevron_right


Output : 


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :