Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22853

Python for Beginners: Rename Columns in a Dataframe in Python

$
0
0

Pandas dataframes are one of the most efficient data structures to handle tabular data in python. When we import tabular data into dataframes from csv files, we usually need to rename the columns in the dataframes. In this article, we will discuss how we can rename columns in a dataframe in python.

Rename DataFrame Columns Using the rename() Method

The pandas module provides us with the rename() method to rename columns in a dataframe. The rename() method, when invoked on a dataframe, takes a python dictionary as its first input argument. The keys in the dictionary should consist of the original name of the columns that are to be renamed. The values associated with the keys should be the new column names.  After execution, the rename() method returns a new dataframe with the modified name. For example, we can rename the ‘Roll’ column of the given dataframe using the rename() method as shown in the following example.

import pandas as pd
import numpy as np
df=pd.read_csv("demo_file.csv")
print("The dataframe is:")
print(df)
print("The original 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:
     Name  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript
The original column names are:
['Name' 'Roll' 'Language']
The modified column names are:
['Name' 'Roll No.' 'Language']

If you want to rename multiple columns in the dataframe, you can pass the old column names and new column names in 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 original column names are:")
print(df.columns.values)
nameDict={"Name":"Person","Roll":"Roll No."}
df=df.rename(columns=nameDict)
print("The modified column names are:")
print(df.columns.values)

Output:

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

In the above examples, the column names in the original columns aren’t modified. Instead, we get a new dataframe with the modified column names.

You can also rename the columns of the original dataframe. For this, we will use the ‘inplace’ parameter of the rename() method. The ‘inplace’ parameter takes an optional input argument and it has the default value False. Due to this, the column names in the original dataframe aren’t modified. You can set the ‘inplace’ parameter to the value True to modify the column names of the original dataframe as shown below.

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

Output:

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

Suggested Reading: If you are into machine learning, you can read this article on regression in machine learning. You might also like this article on k-means clustering with numerical example.

Rename DataFrame Columns Using a List of Column Names

If you have to rename all the columns of the dataframes at once, you can do it using a python list. For this, we just have to assign the list containing the new dataframe names to the ‘columns’ attribute of the dataframe as shown below.

import pandas as pd
import numpy as np
df=pd.read_csv("demo_file.csv")
print("The dataframe is:")
print(df)
print("The original 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:
     Name  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript
The original column names are:
['Name' 'Roll' 'Language']
The modified column names are:
['Person' 'Roll No.' 'Language']

Conclusion

In this article, we have discussed how to rename columns in a dataframe in python. To know more about python programming, you can read this article on dictionary comprehension in python. You might also like this article on list comprehension in python.

The post Rename Columns in a Dataframe in Python appeared first on PythonForBeginners.com.


Viewing all articles
Browse latest Browse all 22853

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>