How to Change Strings to Lowercase in Pandas DataFrame

You may use the following syntax to change strings to lowercase in Pandas DataFrame:

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

Next, you’ll see the steps to apply the above syntax in practice.

Steps to Change Strings to Lowercase in Pandas DataFrame

Step 1: Create a DataFrame

To begin, let’s 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, columns = ['Fruits', 'Price'])

print (df)

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

Example of a dataset

Step 2: Change the strings to lowercase in Pandas DataFrame

Next, change the strings to lowercase using this template:

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

So the complete Python code would look as follows:

import pandas as pd

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

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

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

print (df)

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

How to Change Strings to Lowercase in Pandas DataFrame

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, columns = ['Fruits', 'Price'])

print (df)

This is how the new DataFrame would look like:

Example of a dataset in Python

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, columns = ['Fruits', 'Price'])

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

print (df)

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

Change Strings to Lowercase in Pandas DataFrame

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, columns = ['Fruits', 'Price'])

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:

How to Change Strings to Lowercase in Pandas DataFrame

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, columns = ['Fruits', 'Price'])

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

print (df)

Here is the result:

How to Change Strings to Lowercase in Pandas DataFrame

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