Wednesday, May 1, 2024
HomePythonRename Column by Index in Dataframes

Rename Column by Index in Dataframes


Dataframes are used to deal with tabular information in python. On this article, we’ll focus on how we are able to rename a column by index in dataframes in python.

Change Column Identify Utilizing Index Quantity

We will entry the column names in a dataframe utilizing the ‘columns’ attribute. The columns attribute of a dataframe incorporates an Index object. The Index object incorporates a listing of column names as you possibly can see 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 column object is:")
print(df.columns)

Output:

The dataframe is:
     Identify  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript
The column object is:
Index(['Name', 'Roll', 'Language'], dtype="object")

You may entry the array of column names utilizing the ‘values’ attribute of the Index object 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 column object is:")
print(df.columns)
print("The columns 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 column object is:
Index(['Name', 'Roll', 'Language'], dtype="object")
The columns are:
['Name' 'Roll' 'Language']

To rename the column by index within the dataframe, we are able to modify the array within the values attribute. As an illustration, you possibly can change the identify of the primary column utilizing index 0 of the values array 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 column object is:")
print(df.columns)
print("The columns are:")
print(df.columns.values)
df.columns.values[0]="First Identify"
print("The modified column object is:")
print(df.columns)
print("The modified columns 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 column object is:
Index(['Name', 'Roll', 'Language'], dtype="object")
The columns are:
['Name' 'Roll' 'Language']
The modified column object is:
Index(['First Name', 'Roll', 'Language'], dtype="object")
The modified columns are:
['First Name' 'Roll' 'Language']

On this strategy, we can’t change a number of column names directly. To vary a number of column names, you must rename every column identify one after the other 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 column object is:")
print(df.columns)
print("The columns are:")
print(df.columns.values)
df.columns.values[0]="First Identify"
df.columns.values[1]="Roll Quantity"
print("The modified column object is:")
print(df.columns)
print("The modified columns 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 column object is:
Index(['Name', 'Roll', 'Language'], dtype="object")
The columns are:
['Name' 'Roll' 'Language']
The modified column object is:
Index(['First Name', 'Roll Number', 'Language'], dtype="object")
The modified columns are:
['First Name' 'Roll Number' 'Language']

We will additionally change a number of column names by index directly utilizing the rename() methodology. Allow us to focus on this strategy. 

Urged Studying: If you’re into machine studying, you possibly can learn this text on regression in machine studying. You may also like this text on k-means clustering with numerical instance.

Change Column Identify Utilizing rename() Technique in a DataFrame

We will use the rename() methodology to rename a number of columns utilizing the index numbers. The rename() methodology, when invoked on a dataframe, takes a dictionary as its enter argument. The dictionary ought to comprise the column names that should be renamed because the keys. The brand new column names must be the values related to the unique keys. After execution, the rename() methodology returns a brand new dataframe with the modified column names. 

To change the column names utilizing the index quantity and rename() methodology, we’ll first get hold of the array of column names utilizing the columns.values attribute of the dataframe. After that, we’ll create a dictionary with column names as keys and the brand new column names as related values for the keys. Then, we’ll cross the dictionary to the rename() methodology. After execution, the rename() methodology will return the dataframe with modified column names 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 column object is:")
print(df.columns)
print("The columns are:")
print(df.columns.values)
nameDict={"Identify":"First Identify","Roll":"Roll No."}
df=df.rename(columns=nameDict)
print("The modified column object is:")
print(df.columns)
print("The modified columns 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 column object is:
Index(['Name', 'Roll', 'Language'], dtype="object")
The columns are:
['Name' 'Roll' 'Language']
The modified column object is:
Index(['First Name', 'Roll No.', 'Language'], dtype="object")
The modified columns are:
['First Name' 'Roll No.' 'Language']

Conclusion

On this article, we’ve got mentioned easy methods to rename column by index in dataframes in python. To know extra about python programming, you possibly can learn this text on dictionary comprehension in python. You may also like this text on checklist comprehension in python.

Beneficial Python Coaching

Course: Python 3 For Learners

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments