Pandas How To Uncategorized How to replace part of string in Pandas

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:

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')


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.

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post