Change Strings to Lowercase in Pandas DataFrame

To change strings to lowercase in Pandas DataFrame:

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

Steps to Change Strings to Lowercase in Pandas DataFrame

Step 1: Create a DataFrame

To begin, create a simple DataFrame with 5 fruits (all in uppercase) and their prices:

import pandas as pd

data = {'Fruits': ['BANANA', 'APPLE', 'MANGO', 'WATERMELON', 'PEAR'],
        'Price': [0.5, 1, 1.5, 2.5, 1]
        }

df = pd.DataFrame(data)

print(df)

As you can see, all the 5 fruits are captured in uppercase:

       Fruits  Price
0      BANANA    0.5
1       APPLE    1.0
2       MANGO    1.5
3  WATERMELON    2.5
4        PEAR    1.0

Step 2: Change the strings to lowercase in Pandas DataFrame

Next, change the strings to lowercase using this template:

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

So the complete Python code would be:

import pandas as pd

data = {'Fruits': ['BANANA', 'APPLE', 'MANGO', 'WATERMELON', 'PEAR'],
        'Price': [0.5, 1, 1.5, 2.5, 1]
        }

df = pd.DataFrame(data)

df['Fruits'] = df['Fruits'].str.lower()

print(df)

Run the code, and you’ll notice that the 5 fruits are now in lower case:

       Fruits  Price
0      banana    0.5
1       apple    1.0
2       mango    1.5
3  watermelon    2.5
4        pear    1.0

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

For example, let’s create a new DataFrame, where each value in the DataFrame, under the ‘Fruits’ column, would contain multiple words/fruits in uppercase:

import pandas as pd

data = {'Fruits': ['BANANA AND BLUEBERRY', 'APPLE AND CHERRY', 'MANGO AND PINEAPPLE', 'WATERMELON AND PAPAYA',
                   'PEAR AND COCONUT'],
        'Price': [2.5, 3, 5.5, 4.5, 4]
        }

df = pd.DataFrame(data)

print(df)

This is how the new DataFrame would look like:

                  Fruits  Price
0   BANANA AND BLUEBERRY    2.5
1       APPLE AND CHERRY    3.0
2    MANGO AND PINEAPPLE    5.5
3  WATERMELON AND PAPAYA    4.5
4       PEAR AND COCONUT    4.0

In that case, the logic to change the strings to lowercase is the same:

import pandas as pd

data = {'Fruits': ['BANANA AND BLUEBERRY', 'APPLE AND CHERRY', 'MANGO AND PINEAPPLE', 'WATERMELON AND PAPAYA',
                   'PEAR AND COCONUT'],
        'Price': [2.5, 3, 5.5, 4.5, 4]
        }

df = pd.DataFrame(data)

df['Fruits'] = df['Fruits'].str.lower()

print(df)

You’ll now notice that all the fruits are in lower case:

                  Fruits  Price
0   banana and blueberry    2.5
1       apple and cherry    3.0
2    mango and pineapple    5.5
3  watermelon and papaya    4.5
4       pear and coconut    4.0

Capitalize the First Character of Each Word

You can capitalize the first character of each word using str.title() as captured below:

import pandas as pd

data = {'Fruits': ['BANANA AND BLUEBERRY', 'APPLE AND CHERRY', 'MANGO AND PINEAPPLE', 'WATERMELON AND PAPAYA',
                   'PEAR AND COCONUT'],
        'Price': [2.5, 3, 5.5, 4.5, 4]
        }

df = pd.DataFrame(data)

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

print(df)

As you may observe, the first character of each word is now capitalized under the ‘Fruits’ column:

                  Fruits  Price
0   Banana And Blueberry    2.5
1       Apple And Cherry    3.0
2    Mango And Pineapple    5.5
3  Watermelon And Papaya    4.5
4       Pear And Coconut    4.0

Only Capitalize the First Character of the First Word

Now let’s capitalize the first character of the first word, while keeping everything else in lowercase:

import pandas as pd

data = {'Fruits': ['BANANA AND BLUEBERRY', 'APPLE AND CHERRY', 'MANGO AND PINEAPPLE', 'WATERMELON AND PAPAYA',
                   'PEAR AND COCONUT'],
        'Price': [2.5, 3, 5.5, 4.5, 4]
        }

df = pd.DataFrame(data)

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

print(df)

Here is the result:

                  Fruits  Price
0   Banana and blueberry    2.5
1       Apple and cherry    3.0
2    Mango and pineapple    5.5
3  Watermelon and papaya    4.5
4       Pear and coconut    4.0

You can find out more about str.lower() by visiting the Pandas Documentation.

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

Leave a Comment