Renaming column names in Python with pandas DataFrame

Renaming column names in Python with pandas DataFrame

To rename column names in Python DataFrame, you can use the rename() function.

When dealing with the dataset the column name should be easy to understand as well as it should be flexible to read. Generally, in the data analysis process, the column name is considered as most important as they were providing the meaning to data. Considering these needs we generally rename the column name.

rename() and set_axis() are the commonly used methods in renaming the column names where we can change one particular column name or all the column names at the same time.

Method 1: rename() Function in Python

We can use this function to rename the column in the pandas DataFrame. This can be done with the concept of a dictionary in Python. In Python dictionaries, we are mapping the existing column name as the key and the new column name as the value.

Example:

import pandas as pd

# dataset

df = pd.DataFrame({‘price’:[100,200] , ‘qty’:[4,5]})

# changing column name by rename() function

df.rename(column={‘price’: ‘product price’, ‘qty’: ‘product qty’})

print(df)

Output:

Method 2: Rename column using set_axis () in python

In Python DataFrame, renaming all the column names can be done with the set_axis() function by listing the new column names along with the axis. In which 0 for rows and 1 for columns. The index can also be changed using this function.

Example

import pandas as pd

# DataFrame

df = pd.DataFrame({‘A’:[156,157,158], ’B’:[4000,5000,6000]})

# rename by set_axis

df = df.set_axis([‘id’, ‘salary’], axis = 1, inplace = False)

print(df)

Output:

Method 3: Insertion of characters in column name using Python

In a large dataset, the chance of unwanted characters and spaces can be high, leading to messy data, or sometimes it may contain pointless words, this can also occur in the column name. This can be avoided by adding some characters to make it meaningful.

Example

import pandas as pd

# sample data with space between column name

df = pd.DataFrame({‘product name’ :[“soap”, “shampoo”, “oil”], ‘expiry date’ :[‘2024’,  ‘2024’, ‘2025’]})

# replace space with hyphen

df.column = df.column.str.replace(‘  ’, ‘-’)

print(df)

Output:

In this example, the character is placed between the spaces in the column name.

Method 4: add_prefix() and add_suffix() function in Python

add_prefix() function is commonly used to rename the column by adding another word at the beginning of the column name and the add_suffix() function is used to attach any other word at the end of it.

Example: Using add_prefix() function

import pandas as pd

# DataFrame

df = pd.DataFrame({‘name’ :[Ram,Rahul], ‘age’ :[40, 52]})

# rename by add_prefix

prefixed_column =  df.add_prefix(‘info_’)

print(“prefixed DataFrame : /n”, prefixed_column)

Output:

Example: Using add_suffix() function

import pandas as pd

# dataFrame

df = pd.DataFrame({‘A’ : [“India” ,”Pakistan”], ‘B’ : [“Singapore”, “Malaysia”]})

# rename by add_suffix

suffixed_column = df.add_suffix(‘_country’)

print(“suffixed DataFrame: /n”, suffixed_column)

Output:

Method 5: Changing column name while importing CSV file in Python

While reading the csv file we can change the column name using the function read_csv. In this function, the name parameter is used to list the new column names as an alternative to the name in the CSV file.

Example:

import panda as pd

# read csv file and rename column

df = pd.read_csv(“my_file.csv”, names = [‘laptop’,  ‘pc’, ‘mobile’], header = None)

‘header = None’ is used to specify that the particular file does not have a header row with the column name.

Conclusion

Now, you must be aware of the methods that are commonly used to rename column names in Python DataFrames. You have also learned about changing column names while importing CSV files in Python.

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

Full Stack Developer Course Banner