Pandas Dataframes are used to deal with tabular information in python. On this article, we’ll talk about how we will rename particular columns in a dataframe in python.
Rename Particular Columns in a Dataframe by Index
We will entry the column names in a pandas dataframe utilizing the ‘columns
’ attribute. The ‘columns
’ attribute, when invoked on a dataframe object, returns an Index object. You possibly can observe this within the following instance.
import pandas as pd
df1 = pd.read_csv('student_details.csv')
print("The dataframe is:")
print(df1)
columns = df1.columns
print("The column object is:")
print(columns)
Output:
The dataframe is:
Title Roll Quantity Topic
0 Aditya 12 Python
1 Sam 23 Java
2 Chris 11 C++
3 Joel 10 JavaScript
4 Mayank 5 Typescript
The column object is:
Index(['Name', 'Roll Number', ' Subject'], dtype="object")
The Index object comprises the ‘values
’ attribute by which all of the column names are saved in an array as proven beneath.
import pandas as pd
df1 = pd.read_csv('student_details.csv')
print("The dataframe is:")
print(df1)
columns = df1.columns
print("The column object is:")
print(columns)
print("The column worth is")
print(columns.values)
Output:
The dataframe is:
Title Roll Quantity Topic
0 Aditya 12 Python
1 Sam 23 Java
2 Chris 11 C++
3 Joel 10 JavaScript
4 Mayank 5 Typescript
The column object is:
Index(['Name', 'Roll Number', ' Subject'], dtype="object")
The column worth is
['Name' 'Roll Number' ' Subject']
To rename particular columns within the dataframe, we will change the weather of the values array. For example, we will change the worth “Roll Quantity”
to “Registration Quantity”
within the values array as follows.
df1.columns.values[1] = "Registration Quantity"
The above change is mirrored within the column names of the pandas dataframe. Thus, the “Roll Quantity”
column identify can be modified to “Registration Quantity”
column identify within the dataframe. You possibly can observe this within the following instance.
import pandas as pd
df1 = pd.read_csv('student_details.csv')
print("The dataframe earlier than modification is:")
print(df1)
df1.columns.values[1] = "Registration Quantity"
print("The dataframe after modification is:")
print(df1)
Output:
The dataframe earlier than modification is:
Title Roll Quantity Topic
0 Aditya 12 Python
1 Sam 23 Java
2 Chris 11 C++
3 Joel 10 JavaScript
4 Mayank 5 Typescript
The dataframe after modification is:
Title Registration Quantity Topic
0 Aditya 12 Python
1 Sam 23 Java
2 Chris 11 C++
3 Joel 10 JavaScript
4 Mayank 5 Typescript
To alter a number of column names directly, you may also change a number of values within the values array. The change can be mirrored within the dataframe too.
Steered Studying: Regression in Machine Studying
Rename Particular Columns in a Dataframe Utilizing the rename() Methodology
As an alternative of utilizing the ‘values
’ array, we will use the rename()
technique to rename particular columns in a dataframe. The rename()
technique, when invoked on a dataframe, takes a dictionary mapping as its enter argument. The mapping ought to comprise the column identify that must be renamed as key, and the brand new column identify must be the worth related to the important thing within the dictionary. After execution, the rename()
technique will return a brand new dataframe by which the particular column given within the enter dictionary is renamed. You possibly can observe this within the following instance.
import pandas as pd
df1 = pd.read_csv('student_details.csv')
print("The dataframe earlier than modification is:")
print(df1)
new_df = df1.rename(columns={'Roll Quantity': "Registration Quantity"})
print("The dataframe after modification is:")
print(new_df)
Output:
The dataframe earlier than modification is:
Title Roll Quantity Topic
0 Aditya 12 Python
1 Sam 23 Java
2 Chris 11 C++
3 Joel 10 JavaScript
4 Mayank 5 Typescript
The dataframe after modification is:
Title Registration Quantity Topic
0 Aditya 12 Python
1 Sam 23 Java
2 Chris 11 C++
3 Joel 10 JavaScript
4 Mayank 5 Typescript
To rename a number of columns, you may move a number of column names and their corresponding modified names as key-value pairs within the python dictionary that’s offered as an enter argument to the rename()
technique as follows.
import pandas as pd
df1 = pd.read_csv('student_details.csv')
print("The dataframe earlier than modification is:")
print(df1)
new_df = df1.rename(columns={' Topic': "Language", 'Roll Quantity': "Registration Quantity"})
print("The dataframe after modification is:")
print(new_df)
Output:
The dataframe earlier than modification is:
Title Roll Quantity Topic
0 Aditya 12 Python
1 Sam 23 Java
2 Chris 11 C++
3 Joel 10 JavaScript
4 Mayank 5 Typescript
The dataframe after modification is:
Title Registration Quantity Language
0 Aditya 12 Python
1 Sam 23 Java
2 Chris 11 C++
3 Joel 10 JavaScript
4 Mayank 5 Typescript
As an alternative of making a brand new dataframe with modified column names, you may also change the column names of the prevailing dataframe utilizing the rename()
technique. For this, we’ll use the ‘inplace
’ parameter of the rename()
technique. The ‘inplace
’ parameter has the default worth False
, which signifies that the unique dataframe isn’t modified and a brand new dataframe is returned after renaming the columns. To switch the column names of the unique dataframe, you may move the worth True
as an enter argument to the ‘inplace
’ parameter as follows.
import pandas as pd
df1 = pd.read_csv('student_details.csv')
print("The dataframe earlier than modification is:")
print(df1)
df1.rename(columns={' Topic': "Language", 'Roll Quantity': "Registration Quantity"},inplace=True)
print("The dataframe after modification is:")
print(df1)
Output:
The dataframe earlier than modification is:
Title Roll Quantity Topic
0 Aditya 12 Python
1 Sam 23 Java
2 Chris 11 C++
3 Joel 10 JavaScript
4 Mayank 5 Typescript
The dataframe after modification is:
Title Registration Quantity Language
0 Aditya 12 Python
1 Sam 23 Java
2 Chris 11 C++
3 Joel 10 JavaScript
4 Mayank 5 Typescript
Within the above instance, you may observe that the unique dataframe has been modified after utilizing the ‘inplace
’ parameter.
Conclusion
On this article, we’ve got mentioned varied methods to rename particular columns in a dataframe. To know extra about python programming, you may learn this text on record comprehension in python. You may additionally like this text on dictionary comprehension in python.
Keep tuned for extra informative articles. Glad Studying!
Associated
Beneficial Python Coaching
Course: Python 3 For Newcomers
Over 15 hours of video content material with guided instruction for learners. Learn to create actual world functions and grasp the fundamentals.