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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# 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.