[英]Replace whole string if it contains substring in pandas
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']})
You can use str.contains
to mask the rows that contain 'ball' and then overwrite with the new value: 您可以使用str.contains
来屏蔽包含'ball'的行,然后使用新值覆盖:
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: 为了使它不区分大小写传递`case = False:
df.loc[df['sport'].str.contains('ball', case=False), 'sport'] = 'ball sport'
一个不同的str.contains
df['support'][df.name.str.contains('ball')] = 'ball support'
You can use a lambda function also:您也可以使用 lambda 函数:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.