Friday, April 26, 2024
HomePythonRename Columns in a Dataframe in Python

Rename Columns in a Dataframe in Python


Pandas dataframes are some of the environment friendly information constructions to deal with tabular information in python. After we import tabular information into dataframes from csv recordsdata, we often have to rename the columns within the dataframes. On this article, we are going to talk about how we will rename columns in a dataframe in python.

Rename DataFrame Columns Utilizing the rename() Technique

The pandas module gives us with the rename() technique to rename columns in a dataframe. The rename() technique, when invoked on a dataframe, takes a python dictionary as its first enter argument. The keys within the dictionary ought to include the unique title of the columns which might be to be renamed. The values related to the keys needs to be the brand new column names.  After execution, the rename() technique returns a brand new dataframe with the modified title. For instance, we will rename the ‘Roll’ column of the given dataframe utilizing the rename() technique as proven within the following instance.

import pandas as pd
import numpy as np
df=pd.read_csv("demo_file.csv")
print("The dataframe is:")
print(df)
print("The unique column names are:")
print(df.columns.values)
nameDict={"Roll":"Roll No."}
df=df.rename(columns=nameDict)
print("The modified column names are:")
print(df.columns.values)

Output:

The dataframe is:
     Identify  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript
The unique column names are:
['Name' 'Roll' 'Language']
The modified column names are:
['Name' 'Roll No.' 'Language']

If you wish to rename a number of columns within the dataframe, you may go the previous column names and new column names within the dictionary as follows.

import pandas as pd
import numpy as np
df=pd.read_csv("demo_file.csv")
print("The dataframe is:")
print(df)
print("The unique column names are:")
print(df.columns.values)
nameDict={"Identify":"Particular person","Roll":"Roll No."}
df=df.rename(columns=nameDict)
print("The modified column names are:")
print(df.columns.values)

Output:

The dataframe is:
     Identify  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript
The unique column names are:
['Name' 'Roll' 'Language']
The modified column names are:
['Person' 'Roll No.' 'Language']

Within the above examples, the column names within the authentic columns aren’t modified. As a substitute, we get a brand new dataframe with the modified column names.

You can too rename the columns of the unique dataframe. For this, we are going to use the ‘inplace’ parameter of the rename() technique. The ‘inplace’ parameter takes an elective enter argument and it has the default worth False. Attributable to this, the column names within the authentic dataframe aren’t modified. You possibly can set the ‘inplace’ parameter to the worth True to change the column names of the unique dataframe as proven under.

import pandas as pd
import numpy as np
df=pd.read_csv("demo_file.csv")
print("The dataframe is:")
print(df)
print("The unique column names are:")
print(df.columns.values)
nameDict={"Identify":"Particular person","Roll":"Roll No."}
df.rename(columns=nameDict,inplace=True)
print("The modified column names are:")
print(df.columns.values)

Output:

The dataframe is:
     Identify  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript
The unique column names are:
['Name' 'Roll' 'Language']
The modified column names are:
['Person' 'Roll No.' 'Language']

Recommended Studying: In case you are into machine studying, you may learn this text on regression in machine studying. You may also like this text on k-means clustering with numerical instance.

Rename DataFrame Columns Utilizing a Listing of Column Names

If it’s important to rename all of the columns of the dataframes directly, you are able to do it utilizing a python checklist. For this, we simply should assign the checklist containing the brand new dataframe names to the ‘columns’ attribute of the dataframe as proven under.

import pandas as pd
import numpy as np
df=pd.read_csv("demo_file.csv")
print("The dataframe is:")
print(df)
print("The unique column names are:")
print(df.columns.values)
df.columns=['Person', 'Roll No.', 'Language']
print("The modified column names are:")
print(df.columns.values)

Output:

The dataframe is:
     Identify  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript
The unique column names are:
['Name' 'Roll' 'Language']
The modified column names are:
['Person' 'Roll No.' 'Language']

Conclusion

On this article, we now have mentioned how one can rename columns in a dataframe in python. To know extra about python programming, you may learn this text on dictionary comprehension in python. You may also like this text on checklist comprehension in python.

Advisable Python Coaching

Course: Python 3 For Learners

Over 15 hours of video content material with guided instruction for novices. Discover ways to create actual world purposes and grasp the fundamentals.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments