0
0
Pandasdata~5 mins

str.replace() for substitution in Pandas

Choose your learning style9 modes available
Introduction

We use str.replace() to change parts of text in data. It helps fix or update words or characters easily.

You want to fix typos in a list of names.
You need to change date formats in a text column.
You want to remove unwanted characters like extra spaces or symbols.
You want to replace abbreviations with full words in a dataset.
You want to clean data before analysis by standardizing text.
Syntax
Pandas
Series.str.replace(pat, repl, n=-1, case=None, regex=True)

pat is the pattern or text you want to replace.

repl is the new text you want to put instead.

Examples
Replace all 'John' with 'Jonathan' in the 'Name' column.
Pandas
df['Name'].str.replace('John', 'Jonathan')
Change all slashes '/' to dashes '-' in the 'Date' column.
Pandas
df['Date'].str.replace('/', '-')
Remove all spaces from the 'Code' column.
Pandas
df['Code'].str.replace(' ', '')
Replace only the first 'a' with 'A' in each string of the 'Text' column.
Pandas
df['Text'].str.replace('a', 'A', n=1)
Sample Program

This program creates a table with names, dates, and codes. It then fixes the names by changing 'John' to 'Jonathan', changes date slashes to dashes, and removes spaces from codes.

Pandas
import pandas as pd

# Create a sample DataFrame
sample_data = {'Name': ['John Doe', 'Jane Smith', 'Johnny Appleseed', 'Johnathan Davis'],
               'Date': ['2023/01/01', '2023/02/15', '2023/03/20', '2023/04/25'],
               'Code': ['A 123', 'B 456', 'C 789', 'D 012']}
df = pd.DataFrame(sample_data)

print('Original DataFrame:')
print(df)

# Replace 'John' with 'Jonathan' in 'Name'
df['Name'] = df['Name'].str.replace('John', 'Jonathan')

# Replace '/' with '-' in 'Date'
df['Date'] = df['Date'].str.replace('/', '-')

# Remove spaces in 'Code'
df['Code'] = df['Code'].str.replace(' ', '')

print('\nDataFrame after str.replace() operations:')
print(df)
OutputSuccess
Important Notes

Time complexity: Usually O(n) where n is the number of strings, because it checks each string once.

Space complexity: O(n) for the new strings created after replacement.

Common mistake: Forgetting that str.replace() returns a new Series and does not change the original unless assigned back.

Use str.replace() when you want to change text inside strings. For removing rows or columns, use other methods.

Summary

str.replace() helps change parts of text in pandas columns easily.

It works on each string in a column and returns a new column with changes.

Remember to assign the result back to keep the changes.