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?

You can use the pandas series .str.upper()
method to rename all column names to uppercase in a pandas dataframe. Use the following steps –
- Access the column names using
columns
attribute of the dataframe. - Change the column names to uppercase using the
.str.upper()
method. - 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:

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:

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:

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 –
- Pandas – Change Column Names to Lowercase
- Pandas – Rename Categories in Category Column
- Pandas – Add Column From Another Dataframe
- Pandas – Add an Empty Column to a DataFrame
- Pandas dataframe describe() function
- Pandas – Get Value of a Cell in Dataframe
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.