Pandas - Common String Methods

By Hemanta Sundaray on 2021-08-13

Let's read the budget.xlsx workbook into a DataFrame:

import pandas as pd

data = pd.read_excel("budget.xlsx")

data

Output:

Budget

upper()

  • Series.str.upper: Converts all characters to uppercase.
data["Zone"].str.upper()

Output:

Uppercase

lower()

  • Series.str.lower: Converts all characters to lowercase.
data["Store Code"].str.lower()

Output:

Lowercase

title()

  • Series.str.title: Converts the first character of each word to uppercase and remaining to lowercase.
data["City"].str.title()

Output:

Title

len()

  • Series.str.len: Returns the length of each element in the Series.
data["City"].str.len()

Len

replace()

We will replace the character D in all the elements in the Store Code column with the character D.

data["Store Code"].str.replace("Z", "D")

Output:

Replace

Join the Newsletter