• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Change Column Name in pandas DataFrame

Change Column Name in pandas DataFrame

October 11, 2022 Leave a Comment

To change a column’s name in a pandas DataFrame in Python, the easiest way is that you can use the pandas rename() function.

import pandas as pd

df = pd.DataFrame({"some_column": [1, 2, 3]})

print(df)

df.rename(columns={"some_column": "changed_name"}, inplace=True)

print(df) 

#Output:
   some_column
0            1
1            2
2            3

   changed_name
0             1
1             2
2             3

When working with collections of data, the ability to easily be able to change the names of your variables and datasets is useful.

One such case is if you want to change a column name in a pandas DataFrame.

To change a column’s name in a pandas DataFrame in Python, the easiest way is that you can use the pandas rename() function.

To change a column’s name with rename(), just pass a dictionary to the ‘columns’ parameter with keys and values which represent the columns you want to rename and the new names for the columns, respectively.

Below is a simple example showing you how to rename and change the names of columns in a pandas DataFrame in Python.

import pandas as pd

df = pd.DataFrame({"some_column": [1, 2, 3]})

print(df)

df.rename(columns={"some_column": "changed_name"}, inplace=True)

print(df) 

#Output:
   some_column
0            1
1            2
2            3

   changed_name
0             1
1             2
2             3

Change Column Names of Multiple Columns in pandas

If you want to change the name of multiple columns, you can do this with the pandas rename() function.

To change the name of multiple columns, you just need to pass more key/value pairs to the ‘columns’ parameter.

Below shows a simple example of how you can change the names of multiple columns in a pandas DataFrame in Python.

import pandas as pd

df = pd.DataFrame({"one": [1, 2, 3], "two":[2,3,4]})

print(df)

df.rename(columns={"one": "first", "two":"second"}, inplace=True)

print(df) 

#Output:
   one  two
0    1    2
1    2    3
2    3    4

   first  second
0      1       2
1      2       3
2      3       4

Hopefully this article has been useful for you to learn how to change column names in pandas in your Python code.

Other Articles You'll Also Like:

  • 1.  Count Number of Keys in Dictionary in Python
  • 2.  Add Seconds to Datetime Variable Using Python timedelta() Function
  • 3.  Change Python Turtle Shape Fill Color with fillcolor() Function
  • 4.  Creating a Random Color Turtle in Python
  • 5.  Remove Leading and Trailing Characters from String with strip() in Python
  • 6.  Get Current Year in Python
  • 7.  Writing Multiple Lines Lambda Expression in Python
  • 8.  How to Capitalize the First Letter of Every Word in Python
  • 9.  Python Get Yesterday’s Date
  • 10.  Selenium minimize_window() Function to Minimize Window in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x