Skip to Content

Pandas – Change Column Names to Uppercase

In this tutorial, we will look at how to change the column names of a pandas dataframe to uppercase with the help of some examples.

How to convert column names to uppercase in a pandas dataframe?

pandas change all column names to uppercase

You can use the pandas series .str.upper() method to rename all column names to uppercase in a pandas dataframe. Use the following steps –

  1. Access the column names using columns attribute of the dataframe.
  2. Change the column names to uppercase using the .str.upper() method.
  3. Reset the column names of the dataframe to uppercase column names from above.

The following is the syntax –

# change all column names to uppercase
df.columns = df.columns.str.upper()

Examples

Let’s now look at some examples. First, we will create a dataframe that we will be using throughout this tutorial –

import pandas as pd

# employee data
data = {
    "Name": ["Jim", "Dwight", "Angela", "Tobi"],
    "Age": [26, 28, 27, 32],
    "Department": ["Sales", "Sales", "Accounting", "HR"]
}

# create pandas dataframe
df = pd.DataFrame(data)

# display the dataframe
df

Output:

dataframe with employee data

Here, we created a dataframe with data of some employees in an office. You can see that the column names in the above dataframe are – “Name”, “Age”, and “Department”.

Example 1 – Convert all column names to uppercase

Let’s change all the column names to their respective uppercase values. We will use the syntax mentioned above using the .str.upper() function.

# change all column names to uppercase
df.columns = df.columns.str.upper()
# display the dataframe
df

Output:

employee dataframe with all columns in uppercase

You can see that now all the column names of the dataframe df are in uppercase.

Example 2 – Convert a specific column name to uppercase

If you want to change the name of a specific column to uppercase, you can use the pandas dataframe rename() function. Use the following syntax –

df = df.rename(columns={"OldName":"NewName"})

Let’s change the column name of the above dataframe to its original values. And then change only the column name “Department” to its uppercase value.

# reset column names
df.columns = ["Name", "Age", "Department"]
# change column name "Department" to uppercase
df = df.rename(columns={"Department":"DEPARTMENT"})
# display the dataframe
df

Output:

employee dataframe with only department column name in uppercase

You can see that the column name “Department” is now “DEPARTMENT”.

Summary

In this tutorial, we looked at how to change the column names of a dataframe to uppercase. The following are the key takeaways –

  • Use .str.upper() method to change the column names (of all the columns) to uppercase.
  • If you want only want to change the column name (to uppercase or any other value) for a specific column (or columns), use the pandas dataframe rename() function.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.