• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
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.  pandas drop – Drop Rows or Columns from DataFrame
  • 2.  Using Python to Remove Apostrophe From String
  • 3.  Get First Digit in Number Using Python
  • 4.  pandas Drop Columns – Delete Columns from a DataFrame
  • 5.  pandas interpolate() – Fill NaN Values with Interpolation in DataFrame
  • 6.  Using Python to Remove Commas from String
  • 7.  Using Python to Count Number of Lines in String
  • 8.  Sign Function in Python – Get Sign of Number
  • 9.  Python min() function – Get Minimum of List or Dictionary with min() Function
  • 10.  Count Primes Python – How to Count Number of Primes in a List

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 *

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