56

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'}})
Zero
  • 74,117
  • 18
  • 147
  • 154
nicofilliol
  • 675
  • 1
  • 5
  • 6

5 Answers5

91

You can use str.contains to mask the rows that contain 'ball' and then overwrite with the new value:

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

To make it case-insensitive pass `case=False:

df.loc[df['sport'].str.contains('ball', case=False), 'sport'] = 'ball sport'
EdChum
  • 376,765
  • 198
  • 813
  • 562
  • 2
    `.contains` also accepts regex, so you could add the case-insensitive flag to the string instead of passing `case=False`, like: `.str.contains(r'(?i)ball')`. – Julio Cezar Silva Aug 06 '20 at 18:49
23

You can use apply with a lambda. The x parameter of the lambda function will be each value in the 'sport' column:

df.sport = df.sport.apply(lambda x: 'ball sport' if 'ball' in x else x)
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
15

you can use str.replace

df.sport.str.replace(r'(^.*ball.*$)', 'ball sport')

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

reassign with

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

enter image description here

piRSquared
  • 285,575
  • 57
  • 475
  • 624
3

A different str.contains

 df['support'][df.name.str.contains('ball')] = 'ball support'
Axis
  • 2,066
  • 2
  • 21
  • 40
0

You can use a lambda function also:

data  = {"number": [1, 2, 3, 4, 5], "function": ['IT', 'IT application', 
'IT digital', 'other', 'Digital'] }
df = pd.DataFrame(data)  
df.function = df.function.apply(lambda x: 'IT' if 'IT' in x else x)