pandas :替换字符串中的子字符串 [英] Pandas: replace substring in string
本文介绍了 pandas :替换字符串中的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想替换df
url
icashier.alipay.com/catalog/2758186/detail.aspx
icashier.alipay.com/catalog/2758186/detail.aspx
icashier.alipay.com/catalog/2758186/detail.aspx
vk.com
到aliexpress.com
.
期望输出
aliexpress.com/catalog/2758186/detail.aspx
aliexpress.com/catalog/2758186/detail.aspx
aliexpress.com/catalog/2758186/detail.aspx
vk.com
我尝试df['url'].replace('icashier.alipay.com', 'aliexpress.com', 'inplace=True')
,但它返回empty dataframe
.
推荐答案
使用 replace
和dict
用于替换,参数inplace=True
和regex=True
:
Use replace
with dict
for replacing and parameters inplace=True
and regex=True
:
df['url'].replace({'icashier.alipay.com': 'aliexpress.com'}, inplace=True, 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
这篇关于 pandas :替换字符串中的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文