简体   繁体   English

Pandas:替换字符串中的子字符串

[英]Pandas: replace substring in string

I want to replace substring icashier.alipay.com in column in df我想替换df列中的子字符串icashier.alipay.com

url
icashier.alipay.com/catalog/2758186/detail.aspx
icashier.alipay.com/catalog/2758186/detail.aspx
icashier.alipay.com/catalog/2758186/detail.aspx
vk.com

to aliexpress.com .aliexpress.com

Desire output期望输出

aliexpress.com/catalog/2758186/detail.aspx
aliexpress.com/catalog/2758186/detail.aspx
aliexpress.com/catalog/2758186/detail.aspx
vk.com

I try df['url'].replace('icashier.alipay.com', 'aliexpress.com', 'inplace=True') but it return empty dataframe .我尝试df['url'].replace('icashier.alipay.com', 'aliexpress.com', 'inplace=True')但它返回empty dataframe

Use replace with dict for replacing and regex=True :使用replace with dict进行替换和regex=True

df['url'] = df['url'].replace({'icashier.alipay.com': 'aliexpress.com'}, regex=True)
print (df)
                                          url
0  aliexpress.com/catalog/2758186/detail.aspx
1  aliexpress.com/catalog/2758186/detail.aspx
2  aliexpress.com/catalog/2758186/detail.aspx
3                                      vk.com

use str.replace to replace a substring, replace looks for exact matches unless you pass a regex pattern and param regex=True :使用str.replace替换子字符串, replace查找完全匹配,除非您传递正则表达式模式和参数regex=True

In [25]:
df['url'] = df['url'].str.replace('icashier.alipay.com', 'aliexpress.com')
df['url']

Out[25]:
0    aliexpress.com/catalog/2758186/detail.aspx
1    aliexpress.com/catalog/2758186/detail.aspx
2    aliexpress.com/catalog/2758186/detail.aspx
3                                        vk.com
Name: url, dtype: object

In case someone (like me) needs to replace substring in whole DataFrame:如果有人(比如我)需要替换整个 DataFrame 中的子字符串

df = df.apply(lambda col: col.str.replace('icash...', 'aliex...'))

or just in defined columns (and all others remain unchanged):或者只是在定义的列中(并且所有其他列保持不变):

cols = ['a', 'c'] # list of all columns with value to replace
df = df.apply(lambda col: col.str.replace('icash...', 'aliex...') if col.name in cols else col)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM