How to replace part of string in Pandas

To replace part of a string in a Pandas DataFrame, you can use the str.replace() method with a regular expression. This allows you to replace substrings that match a specific pattern with a new substring. Here’s an example on how to replace part of string:

import pandas as pd

# create a sample dataframe
data = {'name': ['John Smith', 'Jane Doe', 'Bob Johnson'],
'age': [30, 25, 40]}
df = pd.DataFrame(data)

# replace 'Smith' with 'Lee' in the 'name' column
df['name'] = df['name'].str.replace('Smith', 'Lee')

print(df)

This will output:

 name age
0 John Lee 30
1 Jane Doe 25
2 Bob Johnson 40

In the example above, we first created a sample DataFrame with a ‘name’ column containing three different names and an ‘age’ column indicating the age of each person. We then used the str.replace() method to replace the substring ‘Smith’ with ‘Lee’ in the ‘name’ column. The resulting DataFrame was printed to the console.

Note that in the example above, we used a simple string pattern to replace a substring. If you want to replace a more complex pattern, you can use a regular expression. For example, to replace all instances of the pattern ‘J.*n’ (where the name starts with ‘J’ and ends with ‘n’) with the string ‘Jack’, you could use the following code:

df['name'] = df['name'].str.replace(r'J.*n', 'Jack', regex=True)

This will replace all instances of the pattern ‘J.*n’ with ‘Jack’, leaving the other parts of the ‘name’ column unchanged.

Leave a Reply