Convert Column Values to Uppercase in Pandas Dataframe

This article will discuss different ways to convert all values of a Pandas Dataframe column to uppercase in Python.

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 with five rows and four columns,

import pandas as pd

# create dataframe with 5 rows and 4 columns
df= pd.DataFrame({'Roll_Number':[11 ,12, 13, 14, 15],
                  'Name':['sanjay','atharv','ritika','sravan','harsh'],
                  'Age' :[31, 33, 32, 21, 33],
                  'Subjects':['maths','physics','chemistry','python', 'java']})

# Display the Dataframe
print(df)

Output:

   Roll_Number    Name  Age   Subjects
0           11  sanjay   31      maths
1           12  atharv   33    physics
2           13  ritika   32  chemistry
3           14  sravan   21     python
4           15   harsh   33       java

Convert column values to uppercase using str.upper()

Select the column from Dataframe as a Series object using indexing. Then get hold of the underlying string object from the Series object and call the upper() function to convert all the values in that series (dataframe column) to uppercase. The syntax is as follows,

df['column_name'].str.upper()

where, df is the input dataframe and column_name is the name of the dataframe column, whose values need to be converted into uppercase.

Example: In this example, we are going to convert values of ‘Name’ and ‘ Subjects ‘ columns values into uppercase.

import pandas as pd

# create dataframe with 5 rows and 4 columns
df= pd.DataFrame({'Roll_Number':[11 ,12, 13, 14, 15],
                  'Name':['sanjay','atharv','ritika','sravan','harsh'],
                  'Age' :[31, 33, 32, 21, 33],
                  'Subjects':['maths','physics','chemistry','python', 'java']})

# Display the Dataframe
print(df)

# Convert the value of 'Name' column to uppercase
df['Name'] = df['Name'].str.upper()

# Convert the value of 'Subjects' column to uppercase
df['Subjects'] = df['Subjects'].str.upper()

# Display the Dataframe
print(df)

Output:

   Roll_Number    Name  Age   Subjects
0           11  sanjay   31      maths
1           12  atharv   33    physics
2           13  ritika   32  chemistry
3           14  sravan   21     python
4           15   harsh   33       java

   Roll_Number    Name  Age   Subjects
0           11  SANJAY   31      MATHS
1           12  ATHARV   33    PHYSICS
2           13  RITIKA   32  CHEMISTRY
3           14  SRAVAN   21     PYTHON
4           15   HARSH   33       JAVA

Convert column values to uppercase using apply()

Using column name, select a column of Dataframe as a Series object and call the apply() function on that Series object. In the apply() function, pass a function as an argument. The apply() function will call the supplied function for each value in the Series i.e., the Dataframe column.

Now to convert all values in selected column (series), pass the str.upper() function as argument to the apply() function. It will convert all values in column to upper case. Checkout the complete example as follows,

import pandas as pd

# create dataframe with 5 rows and 4 columns
df= pd.DataFrame({'Roll_Number':[11 ,12, 13, 14, 15],
                  'Name':['sanjay','atharv','ritika','sravan','harsh'],
                  'Age' :[31, 33, 32, 21, 33],
                  'Subjects':['maths','physics','chemistry','python', 'java']})

# Display the Dataframe
print(df)

# Convert the value of 'Name' column to uppercase
df['Name'] = df['Name'].apply(str.upper)

# Convert the value of 'Subjects' column to uppercase
df['Subjects'] = df['Subjects'].apply(str.upper)

# Display the Dataframe
print(df)

Output:

   Roll_Number    Name  Age   Subjects
0           11  sanjay   31      maths
1           12  atharv   33    physics
2           13  ritika   32  chemistry
3           14  sravan   21     python
4           15   harsh   33       java

   Roll_Number    Name  Age   Subjects
0           11  SANJAY   31      MATHS
1           12  ATHARV   33    PHYSICS
2           13  RITIKA   32  CHEMISTRY
3           14  SRAVAN   21     PYTHON
4           15   HARSH   33       JAVA

Convert column values to uppercase using map()

Using column name, select a column of Dataframe as a Series object and call the map() function on that Series object. In the map() function, pass a function as an argument. The map() function will call the supplied function on each value in the Series i.e. the Dataframe column.

Now to convert all values in selected column (series), pass the str.upper() function as argument to the map() function. It will convert all values in the column to upper case. Check out the complete example as follows,

import pandas as pd

# create dataframe with 5 rows and 4 columns
df= pd.DataFrame({'Roll_Number':[11 ,12, 13, 14, 15],
                  'Name':['sanjay','atharv','ritika','sravan','harsh'],
                  'Age' :[31, 33, 32, 21, 33],
                  'Subjects':['maths','physics','chemistry','python', 'java']})

# Display the Dataframe
print(df)

# Convert the value of 'Name' column to uppercase
df['Name'] = df['Name'].map(str.upper)

# Convert the value of 'Subjects' column to uppercase
df['Subjects'] = df['Subjects'].map(str.upper)

# Display the Dataframe
print(df)

Output:

   Roll_Number    Name  Age   Subjects
0           11  sanjay   31      maths
1           12  atharv   33    physics
2           13  ritika   32  chemistry
3           14  sravan   21     python
4           15   harsh   33       java

   Roll_Number    Name  Age   Subjects
0           11  SANJAY   31      MATHS
1           12  ATHARV   33    PHYSICS
2           13  RITIKA   32  CHEMISTRY
3           14  SRAVAN   21     PYTHON
4           15   HARSH   33       JAVA

Summary

In this article, we learn about three different ways to convert column values to uppercase in a Pandas dataframe.

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top