我想替换包含特定子字符串的所有字符串。因此,例如,如果我有此数据框:
import pandas as pd df = pd.DataFrame({'name': ['Bob', 'Jane', 'Alice'], 'sport': ['tennis', 'football', 'basketball']})
我可以用字符串“ ball sport”代替足球,如下所示:
df.replace({'sport': {'football': 'ball sport'}})
我想,虽然是替换包含所有ball(在这种情况下football和basketball)与“球运动”。像这样:
ball
football
basketball
df.replace({'sport': {'[strings that contain ball]': 'ball sport'}})
您可以str.contains用来掩盖包含“ ball”的行,然后用新值覆盖:
str.contains
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
要使其不区分大小写,请通过`case = False:
df.loc[df['sport'].str.contains('ball', case=False), 'sport'] = 'ball sport'