pandas replace word begins with contains
df['sport'] = df.sport.str.replace(r'(^.*ball.*$)', 'ball sport')
df
5
2
df.sport = df.sport.apply(lambda x: 'ball sport' if 'ball' in x else x)
Thank you!
2
0
4.83
6
In [71]:
df.loc[df['sport'].str.contains('ball'), 'sport'] = 'ball sport'
df
Out[71]:
name sport
0 Bob tennis
1 Jane ball sport
2 Alice ball sport
Thank you!
6
0
4
7
df.sport.str.replace(r'(^.*ball.*$)', 'ball sport')
0 tennis
1 ball sport
2 ball sport
Name: sport, dtype: object
Thank you!
7
0
Are there any code examples left?
New code examples in category Python