Change Strings to Uppercase in Pandas DataFrame

To change strings to uppercase in Pandas DataFrame:

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

Steps to Change Strings to Uppercase in Pandas DataFrame

Step 1: Create a DataFrame

To start, let’s create a 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)

print(df)

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

  Vegetables  Price
0   broccoli    2.0
1     carrot    3.0
2      onion    1.5
3     celery    2.5
4    spinach    1.0

Step 2: Change the strings to uppercase in Pandas DataFrame

Next, change the strings to uppercase using this template:

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

For our example:

import pandas as pd

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

df = pd.DataFrame(data)

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:

  Vegetables  Price
0   BROCCOLI    2.0
1     CARROT    3.0
2      ONION    1.5
3     CELERY    2.5
4    SPINACH    1.0

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)

print(df)

This is how the new DataFrame would look like:

                 Vegetables  Price
0  broccoli and cauliflower    4.0
1        carrot and cabbage    5.5
2           onion and basil    3.5
3           celery and kale    3.0
4       spinach and lettuce    4.5

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)

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

print(df)

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

                 Vegetables  Price
0  BROCCOLI AND CAULIFLOWER    4.0
1        CARROT AND CABBAGE    5.5
2           ONION AND BASIL    3.5
3           CELERY AND KALE    3.0
4       SPINACH AND LETTUCE    4.5

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)

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

print(df)

The result:

                 Vegetables  Price
0  Broccoli And Cauliflower    4.0
1        Carrot And Cabbage    5.5
2           Onion And Basil    3.5
3           Celery And Kale    3.0
4       Spinach And Lettuce    4.5

Capitalize only the First Character of the First Word

Alternatively, you can capitalize only 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)

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

print(df)

Here is the result:

                 Vegetables  Price
0  Broccoli and cauliflower    4.0
1        Carrot and cabbage    5.5
2           Onion and basil    3.5
3           Celery and kale    3.0
4       Spinach and lettuce    4.5

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.

Leave a Comment