Pandas rename Function Implementation with Steps

Pandas rename Function Implementation with Steps

Pandas is a python library that allows you to create dataframe from the existing datasets. The manipulation of dataframe becomes very easy and efficient when you use the python pandas package. To do so there are many functions provided by the pandas library. The rename() function is one of them. In this post you will know how to implement the pandas rename() function with steps.

Steps to implement pandas rename function

In this section you will know all the steps for implementing rename function. Just follow steps for deep understanding.

Step 1: Import the required library

The first step is to import all the necessary libraries. In this example I am using pandas package only. So lets import it using the import statement.

import pandas as pd

Step 2: Create a dummy dataframe

For the purpose of demonstration I am creating a sample dataframe in which the rename function will be implement.

Use the below lines of code to create a dataframe.

import pandas as pd
data = {"name":["Rob","Maya","Abhishek","Sukesh"],"age":[23,25,27,29]}
df = pd.DataFrame(data)
print(df)
name age
0 Rob 23
1 Maya 25
2 Abhishek 27
3 Sukesh 29

Step 3 : Use the DataFrame.rename() function

The above step has created the dummy dataframe. In this step I will use the rename() function to rename the columns of the dataframe.

Below is the syntax of the rename() function.

DataFrame.rename(mapper=None, index=None, columns=None, axis=None, copy=True, inplace=False, level=None, errors='ignore')

Below is the explanation of the above syntax.

mapper: It accepts a dictionary-like object or a callable function. It maps the old column name to the new column name.
index: It maps the old row index to the new row index.
columns: This parameter accepts a dictionary-like object or a callable function. It maps the old column index to the new column index.
axis: This parameter use axis =o for renaming rows and axis =1 for renaming columns.
copy: Return new dataframe after renaming without modifying the original dataframe.
inplace: This parameter specifies whether to modify the original DataFrame (inplace=True`) or return a new DataFrame (inplace=False).
level:  It specifies the level of the column index to be renamed.
errors: This parameter specifies how errors should be handled, such as renaming a column that does not exist.

In our example I am just simply renaming the existing columns.

Use the same dataframe created in the above step I will execute the below lines of code to rename the columns.

Full Code

import pandas as pd
data = {"name":["Rob","Maya","Abhishek","Sukesh"],"age":[23,25,27,29]}
df = pd.DataFrame(data)
df.rename(columns={"name":"Name"},inplace=True)
print(df)

Output

      Name  age
0       Rob   23
1      Maya   25
2  Abhishek   27
3    Sukesh   29

Rename the age column as Age.

import pandas as pd
data = {"name":["Rob","Maya","Abhishek","Sukesh"],"age":[23,25,27,29]}
df = pd.DataFrame(data)
df.rename(columns={"age":"Age"},inplace=True)
print(df)
       name  Age
0       Rob   23
1      Maya   25
2  Abhishek   27
3    Sukesh   29

Conclusion

Sometime the columns name in the existing dataframe is difficult to handle. So most of the coder use the rename() function to rename the columns to the simple name. You can change the name of a single column or multiple columns using the same rename() function.

I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner