We use str.replace() to change parts of text in data. It helps fix or update words or characters easily.
str.replace() for substitution in 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.
df['Name'].str.replace('John', 'Jonathan')
df['Date'].str.replace('/', '-')
df['Code'].str.replace(' ', '')
df['Text'].str.replace('a', 'A', n=1)
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.
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)
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.
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.