How to Change Strings to Uppercase in Pandas DataFrame

Here is the syntax that you may use to change strings to uppercase in Pandas DataFrame:

df['column name'].str.upper()

Next, you’ll see the steps to apply the above syntax using a practical example.

Steps to Change Strings to Uppercase in Pandas DataFrame

Step 1: Create a DataFrame

To start, let’s create a simple DataFrame with 5 vegetables (all in lowercase) and their prices:

import pandas as pd

data = {'Vegetables': ['broccoli','carrot','onion','celery','spinach'],
        'Price': [2,3,1.5,2.5,1]
        }

df = pd.DataFrame(data, columns = ['Vegetables', 'Price'])

print (df)

As you can see, all the 5 vegetables are captured in lowercase:

Sample of data

Step 2: Change the strings to uppercase in Pandas DataFrame

Next, change the strings to uppercase using this template:

df['column name'].str.upper()

For our example, the complete code to change the strings to uppercase is:

import pandas as pd

data = {'Vegetables': ['broccoli','carrot','onion','celery','spinach'],
        'Price': [2,3,1.5,2.5,1]
        }

df = pd.DataFrame(data, columns = ['Vegetables', 'Price'])


df['Vegetables'] = df['Vegetables'].str.upper()

print (df)

Run the code in Python, and you’ll see that all the 5 vegetables are now converted to uppercase:

How to Change Strings to Uppercase in Pandas DataFrame

What if each value in the DataFrame contains multiple words in lowercase?

For instance, in the DataFrame below, each value under the ‘Vegetables’ column contains multiple words/vegetables in lowercase:

import pandas as pd

data = {'Vegetables': ['broccoli and cauliflower','carrot and cabbage','onion and basil','celery and kale','spinach and lettuce'],
        'Price': [4,5.5,3.5,3,4.5]
        }

df = pd.DataFrame(data, columns = ['Vegetables', 'Price'])

print (df)

This is how the new DataFrame would look like:

Example of a dataset

You can then apply the same logic to change the strings to uppercase:

import pandas as pd

data = {'Vegetables': ['broccoli and cauliflower','carrot and cabbage','onion and basil','celery and kale','spinach and lettuce'],
        'Price': [4,5.5,3.5,3,4.5]
        }

df = pd.DataFrame(data, columns = ['Vegetables', 'Price'])

df['Vegetables'] = df['Vegetables'].str.upper()

print (df)

Once you run the code, you’ll see that all the vegetables are in uppercase:

How to Change Strings to Uppercase in Pandas DataFrame

Capitalize the First Character of Each Word

You can use str.title() to capitalize the first character of each word:

import pandas as pd

data = {'Vegetables': ['broccoli and cauliflower','carrot and cabbage','onion and basil','celery and kale','spinach and lettuce'],
        'Price': [4,5.5,3.5,3,4.5]
        }

df = pd.DataFrame(data, columns = ['Vegetables', 'Price'])

df['Vegetables'] = df['Vegetables'].str.title()

print (df)

You’ll now see that the first character of each word is now capitalized under the ‘Vegetables’ column:

Capitalize the First Character of Each Word using Pandas

Only Capitalize the First Character of the First Word

Alternatively, you can only capitalize the first character of the first Word (while keeping everything else in lowercase):

import pandas as pd

data = {'Vegetables': ['broccoli and cauliflower','carrot and cabbage','onion and basil','celery and kale','spinach and lettuce'],
        'Price': [4,5.5,3.5,3,4.5]
        }

df = pd.DataFrame(data, columns = ['Vegetables', 'Price'])

df['Vegetables'] = df['Vegetables'].str.capitalize()

print (df)

Here is the result:

Capitalize the First Character of the First Word

You may refer to the Pandas Documentation to learn more about str.upper().

You may also want to check the following guide for the steps to change strings to lowercase in Pandas DataFrame.