sk8r sk8r - 6 months ago 43
Python Question

Replace whole string if it contains substring in pandas

Im sorry if this is a very obvious and easy question but I've been searching the internet for a solution but couldn't come up with one that works (and I don't really have much knowledge in python). I want to replace all strings that contain a specific substring. So for example if I have this dataframe:

import pandas as pd
df = pd.DataFrame({'name': ['Bob', 'Jane', 'Alice'],
'sport': ['tennis', 'football', 'basketball']})

I could replace football with the string 'ball sport' like this:

df.replace({'sport': {'football': 'ball sport'}})

What I want though is to replace everything that contains ball (in this case football and basketball) with 'ball sport'. Something like this:

df.replace({'sport': {'[strings that contain ball]': 'ball sport'}})


you can use str.replace'(^.*ball.*$)', 'ball sport')

0        tennis
1    ball sport
2    ball sport
Name: sport, dtype: object

reassign with

df['sport'] ='(^.*ball.*$)', 'ball sport')

enter image description here