To replace a string in a pandas DataFrame or Series, you can use the .replace() method.
Here’s an example:
import pandas as pd
# Create a sample DataFrame
df = pd.DataFrame({'col1': ['apple', 'banana', 'orange'],
'col2': ['red', 'yellow', 'orange']})
# Replace 'orange' with 'grapefruit'
df = df.replace('orange', 'grapefruit')
print(df)
Output:
col1 col2
0 apple red
1 banana yellow
2 grapefruit grapefruit
In this example, we replaced all occurrences of the string ‘orange’ in the DataFrame with ‘grapefruit’.
You can also use regular expressions to replace strings. Here’s an example:
# Replace all strings that start with 'ba' with 'fruit'
df = df.replace('^ba.*', 'fruit', regex=True)
print(df)
Output:
col1 col2
0 apple red
1 fruit yellow
2 grapefruit grapefruit
In this example, we replaced all strings in the DataFrame that start with ‘ba’ with ‘fruit’. The ^ba.* regular expression pattern matches any string that starts with ‘ba’.
