Pandas Dataframes are used to handle tabular data in python. In this article, we will discuss how we can rename specific columns in a dataframe in python.
Rename Specific Columns in a Dataframe by Index
We can access the column names in a pandas dataframe using the ‘columns
’ attribute. The ‘columns
’ attribute, when invoked on a dataframe object, returns an Index object. You can observe this in the following example.
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:
Name Roll Number Subject
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 contains the ‘values
’ attribute in which all the column names are stored in an array as shown below.
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 value is")
print(columns.values)
Output:
The dataframe is:
Name Roll Number Subject
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 value is
['Name' 'Roll Number' ' Subject']
To rename specific columns in the dataframe, we can change the elements of the values array. For instance, we can change the value “Roll Number”
to “Registration Number”
in the values array as follows.
df1.columns.values[1] = "Registration Number"
The above change is reflected in the column names of the pandas dataframe. Thus, the “Roll Number”
column name will be changed to “Registration Number”
column name in the dataframe. You can observe this in the following example.
import pandas as pd
df1 = pd.read_csv('student_details.csv')
print("The dataframe before modification is:")
print(df1)
df1.columns.values[1] = "Registration Number"
print("The dataframe after modification is:")
print(df1)
Output:
The dataframe before modification is:
Name Roll Number Subject
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:
Name Registration Number Subject
0 Aditya 12 Python
1 Sam 23 Java
2 Chris 11 C++
3 Joel 10 JavaScript
4 Mayank 5 Typescript
To change multiple column names at once, you can also change multiple values in the values array. The change will be reflected in the dataframe too.
Suggested Reading: Regression in Machine Learning
Rename Specific Columns in a Dataframe Using the rename() Method
Instead of using the ‘values
’ array, we can use the rename()
method to rename specific columns in a dataframe. The rename()
method, when invoked on a dataframe, takes a dictionary mapping as its input argument. The mapping should contain the column name that needs to be renamed as key, and the new column name should be the value associated with the key in the dictionary. After execution, the rename()
method will return a new dataframe in which the specific column given in the input dictionary is renamed. You can observe this in the following example.
import pandas as pd
df1 = pd.read_csv('student_details.csv')
print("The dataframe before modification is:")
print(df1)
new_df = df1.rename(columns={'Roll Number': "Registration Number"})
print("The dataframe after modification is:")
print(new_df)
Output:
The dataframe before modification is:
Name Roll Number Subject
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:
Name Registration Number Subject
0 Aditya 12 Python
1 Sam 23 Java
2 Chris 11 C++
3 Joel 10 JavaScript
4 Mayank 5 Typescript
To rename multiple columns, you can pass multiple column names and their corresponding changed names as key-value pairs in the python dictionary that is provided as an input argument to the rename()
method as follows.
import pandas as pd
df1 = pd.read_csv('student_details.csv')
print("The dataframe before modification is:")
print(df1)
new_df = df1.rename(columns={' Subject': "Language", 'Roll Number': "Registration Number"})
print("The dataframe after modification is:")
print(new_df)
Output:
The dataframe before modification is:
Name Roll Number Subject
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:
Name Registration Number Language
0 Aditya 12 Python
1 Sam 23 Java
2 Chris 11 C++
3 Joel 10 JavaScript
4 Mayank 5 Typescript
Instead of creating a new dataframe with changed column names, you can also change the column names of the existing dataframe using the rename()
method. For this, we will use the ‘inplace
’ parameter of the rename()
method. The ‘inplace
’ parameter has the default value False
, which means that the original dataframe isn’t modified and a new dataframe is returned after renaming the columns. To modify the column names of the original dataframe, you can pass the value True
as an input argument to the ‘inplace
’ parameter as follows.
import pandas as pd
df1 = pd.read_csv('student_details.csv')
print("The dataframe before modification is:")
print(df1)
df1.rename(columns={' Subject': "Language", 'Roll Number': "Registration Number"},inplace=True)
print("The dataframe after modification is:")
print(df1)
Output:
The dataframe before modification is:
Name Roll Number Subject
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:
Name Registration Number Language
0 Aditya 12 Python
1 Sam 23 Java
2 Chris 11 C++
3 Joel 10 JavaScript
4 Mayank 5 Typescript
In the above example, you can observe that the original dataframe has been modified after using the ‘inplace
’ parameter.
Conclusion
In this article, we have discussed various ways to rename specific columns in a dataframe. To know more about python programming, you can read this article on list comprehension in python. You might also like this article on dictionary comprehension in python.
Stay tuned for more informative articles. Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.