简体   繁体   English

如果它包含熊猫中的子字符串,则替换整个字符串

[英]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'

You can use apply with a lambda. 你可以使用lambda的apply The x parameter of the lambda function will be each value in the 'sport' column: lambda函数的x参数将是'sport'列中的每个值:

df.sport = df.sport.apply(lambda x: 'ball sport' if 'ball' in x else x)

you can use str.replace 你可以使用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

在此输入图像描述

一个不同的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.

相关问题 如果整个字符串包含熊猫数据框中的子字符串,则替换整个字符串 - Replace whole string if it contains substring in pandas dataframe 替换包含大熊猫整个数据框中子字符串的整个字符串 - Replace whole string which contains substring in whole dataframe in pandas 如果在 Pandas 数据框中包含子字符串,则替换整个字符串,但包含值列表 - Replace Whole String if it contains substring in pandas dataframe, but with a list of values 如果熊猫数据框中包含特定的子字符串,请替换该字符串 - Replace string in pandas dataframe if it contains specific substring 如果列表中的字符串在 Pandas DataFrame 列中包含 ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ,如何替换它 - How to replace a string in a list if it contains a substring in Pandas DataFrame column Pandas:替换字符串中的子字符串 - Pandas: replace substring in string 熊猫字符串包含和替换 - Pandas string contains and replace 熊猫替换不替换整个字符串 - Pandas replace not replacing the whole string 如果字符串在 PySpark 中包含某些 substring,则替换字符串 - Replace string if it contains certain substring in PySpark 检查熊猫数据库中的字符串是否包含子字符串并删除 - Check if string in pandas database contains substring and remove
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM