I want to replace all lines containing a specific substring. So, for example, if I have this data framework:
import pandas as pd
df = pd.DataFrame({'name': ['Bob', 'Jane', 'Alice'],
'sport': ['tennis', 'football', 'basketball']})
I could replace soccer with the string “ball sport” as follows:
df.replace({'sport': {'football': 'ball sport'}})
I want, however, to replace everything that contains ball(in this case, footballand basketball) with "ball sport". Something like that:
df.replace({'sport': {'[strings that contain ball]': 'ball sport'}})
source
share