0
0
Pandasdata~5 mins

str.lower() and str.upper() in Pandas

Choose your learning style9 modes available
Introduction

We use str.lower() and str.upper() to change text to all lowercase or all uppercase. This helps us compare or clean text easily.

When you want to compare text without worrying about uppercase or lowercase differences.
When cleaning messy text data that has mixed letter cases.
When preparing text for consistent display or reporting.
When filtering data based on text ignoring case differences.
Syntax
Pandas
df['column_name'].str.lower()
df['column_name'].str.upper()

These methods work on pandas Series with text data.

They return a new Series with all text converted to lower or upper case.

Examples
Converts all names in the 'Name' column to lowercase.
Pandas
df['Name'].str.lower()
Converts all city names in the 'City' column to uppercase.
Pandas
df['City'].str.upper()
Standardizes email addresses to lowercase for easy matching.
Pandas
df['Email'].str.lower()
Sample Program

This code creates a small table with names and cities. It then changes all names to lowercase and all cities to uppercase. Finally, it prints both results.

Pandas
import pandas as pd

data = {'Name': ['Alice', 'BOB', 'ChArLie'], 'City': ['New York', 'los angeles', 'Chicago']}
df = pd.DataFrame(data)

# Convert 'Name' to lowercase
lower_names = df['Name'].str.lower()

# Convert 'City' to uppercase
upper_cities = df['City'].str.upper()

print('Lowercase Names:')
print(lower_names)
print('\nUppercase Cities:')
print(upper_cities)
OutputSuccess
Important Notes

If the column has missing values, these methods will keep them as NaN.

These methods do not change the original DataFrame unless you assign the result back.

Summary

str.lower() and str.upper() change text to all lowercase or uppercase.

They help make text comparisons and cleaning easier.

Use them on pandas Series with text data for consistent results.