Pandas: replace substring in string

前端 未结 2 2009
不知归路
不知归路 2020-11-27 22:07

I want to replace substring icashier.alipay.com in column in df

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


        
相关标签:
2条回答
  • 2020-11-27 22:49

    use str.replace to replace a substring, replace looks for exact matches unless you pass a regex pattern and param 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
    
    0 讨论(0)
  • 2020-11-27 22:56

    Use replace with dict for replacing and 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
    
    0 讨论(0)
提交回复
热议问题