小编典典

熊猫:替换字符串中的子字符串

python

我想替换icashier.alipay.com列中的子字符串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


阅读 212

收藏
2020-12-20

共1个答案

小编典典

使用replacedict更换和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
2020-12-20