Change String Case in pandas


There are several ways to change the case in a pandas String. There is upper, lower, title, capitalize and swap case.

I counted 56 string functions listed at the webpage pandas.Series.str.capitalize.

DataFrame Example

When we focus on a column of a DataFrame, we have a series.

import pandas as pd  # import the pandas library into Python
data = {'firstname': ['Bob', 'Sally', 'Suzie', 'Rohan'],
       'amount': [12, 67, 33, 41],
       'group': ['B', 'A', 'A', 'B']}
df = pd.DataFrame(data)
df

We’ll make the first name all upper case.

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

There are other functions we can use. Title case would capitalize each word in a sentence. Capitalize would capitalize the first word in a sentence.

Leave a Reply