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'}})
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…