Steps to change column name Pandas Dataframe

Name of column plays key role in data analysis. Columns if not named correctly can cause challenges in later stages of analysis. For example some of the programming language cause challenges in case there is blank present in column name. There can be challenges like fields are not correctly marked in the file i.e. data is different and column name is different. In these cases we may have to rename the columns. This article covers steps to change column name Pandas Dataframe.

John has customer data available for his company. He founds out that various column names present in the file contains blank space. He is looking forward to replace blank with special character “_”.

Steps to change column name Pandas Dataframe
  • Step 1: Import all the necessary modules. Here pandas
import pandas as pd
  • Step 2: Use pandas dataframe.rename function to rename column in the dataset. The syntax of the function need list of columns that we are looking forward to rename. First we have to write the old name and the new name. For example for the first field the syntax will be old name that is ‘First Name’ and the second value will be new column name that is :’First_Name’. We can rename as many column as required.
Customer_data2 =Customer_data.rename(columns={'First Name': 'First_Name', 'Last Name': 'Last_Name'})
  • Step 3: Check the new available data if it looks ok. Use dataframe.head to test sample rows of final file.
Customer_data2.head()
Steps to change column name Pandas Dataframe

John will be able to work on the newly available data now.

Thus, John is able to rename column in Pandas Dataframe as per his requirement in Python.

To get top certifications in Python and build your resume visit here. Also, you can read books listed here to build strong knowledge around Python.

Watch our video tutorial to learn more:

 Looking to practice more with this example? Drop us a note, we will email you the Code file: 

    📬 Stay Ahead in Data Science & AI – Subscribe to Newsletter!

    • 🎯 Interview Series: Curated questions and answers for freshers and experienced candidates.
    • 📊 Data Science for All: Simplified articles on key concepts, accessible to all levels.
    • 🤖 Generative AI for All: Easy explanations on Generative AI trends transforming industries.

    💡 Why Subscribe? Gain expert insights, stay ahead of trends, and prepare with confidence for your next interview.

    👉 Subscribe here:

    Related Posts