【问题标题】:Replace certain value that is between two specific words替换两个特定单词之间的某个值
【发布时间】:2019-03-01 11:01:12
【问题描述】:

我正在尝试替换两个特定措辞之间的字符串列中的值

例如,从这个数据框我想改变

 df

 seller_name    url
 Lucas          http://sanyo.mapi/s3/e42390aac371?item_title=Branded%20boys%20Clothing&seller_name=102392852&buyer_item=106822419_1056424990 

到这里

url
http://sanyo.mapi/s3/e42390aac371?item_title=Branded%20boys%20Clothing&seller_name=Lucas&buyer_item=106822419_1056424990 

查看网址中seller_name=部分我换成了真名,我把数字换成了真名。

我想像从seller_name= 更改为第一个,然后从seller_name 看到。

这只是我想做的一个例子,但实际上我的数据框中有很多行,卖家名称中的数字长度并不总是相同

【问题讨论】:

标签: python string python-3.x dataframe


【解决方案1】:
seller_name = 'Lucas'
url = 'http://sanyo.mapi/s3/e42390aac371?item_title=Branded%20boys%20Clothing&seller_name=102392852&buyer_item=106822419_1056424990'
a = url.index('seller_name=')
b = url.index('&', a)
out = url.replace(url[a+12:b],seller_name)
print(out)

试试这个:

【讨论】:

  • OP 处理的是 pandas 而不是核心 python。无论如何,这对 OP 没有帮助
【解决方案2】:

使用apply并将字符串替换为卖家名称

样本df

import pandas as pd
df=pd.DataFrame({'seller_name':['Lucas'],'url':['http://sanyo.mapi/s3/e42390aac371?item_title=Branded%20boys%20Clothing&seller_name=102392852&buyer_item=106822419_1056424990']})

import re
def myfunc(row):
    return(re.sub('(seller_name=\d{1,})','seller_name='+row.seller_name,row.url))
df['url']=df.apply(lambda x: myfunc(x),axis=1)

【讨论】:

    【解决方案3】:

    此解决方案不假定您的查询参数的顺序或您要替换的 ID 的长度。它假定您的查询是&-delimited,并且您有seller_name 参数,存在。

    split_by_amps = url.split('&')
    for i in range(len(split_by_amps)):
        if (split_by_amps[i].startswith('seller_name')):
            split_by_amps[i] += 'seller_name=' + 'Lucas'
            break
    
    result = '&'.join(split_by_amps)
    

    【讨论】:

      【解决方案4】:

      您可以使用正则表达式来替换名称的代码:

      import pandas as pd
      import re
      
      #For example use a dictionary to map codes to names
      seller_dic = {102392852:'Lucas'}
      
      for i in range(len(df['url'])):
          #very careful with this, if a url doesn't have this structure it will throw
          #an error, you may want to handle exceptions
          code = re.search(r'seller_name=\d+&',df['url'][i]).group(0)
          code = code.replace("seller_name=","")
          code = code.replace("&","")
      
          name = 'seller_name=' + seller_dic[code] + '&'
      
          url = re.sub(r'seller_name=\d+&', name, df['url'][i])
      
          df['url'][i] = url
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-21
        • 1970-01-01
        相关资源
        最近更新 更多