Convert Column Names to Uppercase in Pandas Dataframe

This article will discuss different ways to change column names to uppercase in a Pandas Dataframe.

Table Of Contents

A DataFrame is a data structure that stores the data in rows and columns. We can create a DataFrame using pandas.DataFrame() method. Let’s create a dataframe of seven rows and four columns with hard-coded data,

import pandas as pd

#create dataframe with 7 rows and 4 columns
df= pd.DataFrame({'Roll_Number':[1, 2, 3, 4],
                  'Name':['Sravan','Harsha','Jtothika','Mark'],
                  'Age' :[21, 23, 22, 21],
                  'Subjects':['Php','Html','Java','Python']})

# Display the Dataframe
print( df)

Output:

   Roll_Number      Name  Age Subjects
0            1    Sravan   21      Php
1            2    Harsha   23     Html
2            3  Jtothika   22     Java
3            4      Mark   21   Python

In the above Dataframe, the column names are not in uppercase. Let’s see different ways to convert all column labels to uppercase,

Advertisements

Convert Column Names to Uppercase using str.upper()

We will get the dataframe column labels in an Index object by using the columns attribute of the Dataframe. Then using the StringMethods with Index object, we can manipulate column labels. For example, we can call the upper() method to change the column labels to uppercase. Its syntax will be like this,

Syntax:

df.columns.str.upper()

where, df is the input dataframe and columns is the attribute to get the column labels as an Index Object. Then using the StringMethods.upper() we converted all labels to uppercase.

Let’s see an example,

import pandas as pd

#create dataframe with 7 rows and 4 columns
df= pd.DataFrame({'Roll_Number':[1, 2, 3, 4],
                  'Name':['Sravan','Harsha','Jtothika','Mark'],
                  'Age' :[21, 23, 22, 21],
                  'Subjects':['Php','Html','Java','Python']})

# Display the Dataframe
print( df)

# Convert Column Labels to uppercase in Dataframe
df.columns = df.columns.str.upper()

# Display the Dataframe
print( df)

Output:

   Roll_Number      Name  Age Subjects
0            1    Sravan   21      Php
1            2    Harsha   23     Html
2            3  Jtothika   22     Java
3            4      Mark   21   Python

   ROLL_NUMBER      NAME  AGE SUBJECTS
0            1    Sravan   21      Php
1            2    Harsha   23     Html
2            3  Jtothika   22     Java
3            4      Mark   21   Python

It converted all the column labels to uppercase.

Convert Dataframe Column Names to Uppercase using map() & upper()

The columns attribute of the dataframe object returns all column names as a sequence of string objects. We can Iterate over all column names one by one, and for each label, we can call the upper() function. It will change the column name to uppercase. Then we can create a sequence of modified column names and assign it back to the columns attribute of the dataframe. For iteration over column names and applying the upper() function, we can use the map() function. The syntax will be like this,

df.columns=map(str.upper, df.columns)

Example:

import pandas as pd

#create dataframe with 7 rows and 4 columns
df= pd.DataFrame({'Roll_Number':[1, 2, 3, 4],
                  'Name':['Sravan','Harsha','Jtothika','Mark'],
                  'Age' :[21, 23, 22, 21],
                  'Subjects':['Php','Html','Java','Python']})

# Display the Dataframe
print( df)

# Convert Column Labels to uppercase in Dataframe
df.columns=map(str.upper, df.columns)

# Display the Dataframe
print( df)

Output:

   Roll_Number      Name  Age Subjects
0            1    Sravan   21      Php
1            2    Harsha   23     Html
2            3  Jtothika   22     Java
3            4      Mark   21   Python

   ROLL_NUMBER      NAME  AGE SUBJECTS
0            1    Sravan   21      Php
1            2    Harsha   23     Html
2            3  Jtothika   22     Java
3            4      Mark   21   Python

It converted all the column labels to uppercase.

Convert Dataframe Column Names to uppercase using List Comprehension

Iterate over all column names using List Comprehension and during iteration, change the case to uppercase using upper() method. Syntax is as follows,

# Convert Column Labels to uppercase in Dataframe
df.columns= [column.upper() for column in df.columns]

Let’s see an example,

import pandas as pd

#create dataframe with 7 rows and 4 columns
df= pd.DataFrame({'Roll_Number':[1, 2, 3, 4],
                  'Name':['Sravan','Harsha','Jtothika','Mark'],
                  'Age' :[21, 23, 22, 21],
                  'Subjects':['Php','Html','Java','Python']})

# Display the Dataframe
print( df)

# Convert Column Labels to uppercase in Dataframe
df.columns= [column.upper() for column in df.columns]

# Display the Dataframe
print( df)

Output:

   Roll_Number      Name  Age Subjects
0            1    Sravan   21      Php
1            2    Harsha   23     Html
2            3  Jtothika   22     Java
3            4      Mark   21   Python

   ROLL_NUMBER      NAME  AGE SUBJECTS
0            1    Sravan   21      Php
1            2    Harsha   23     Html
2            3  Jtothika   22     Java
3            4      Mark   21   Python

It converted all the column labels to uppercase.

Summary

In this article, we learned about different techniques to change the Pandas Dataframe column names to uppercase in Python.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top