替换两个特定词之间的某些值

时间:2018-09-25 18:28:59

标签: python string python-3.x dataframe

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

例如,我要从此数据框中更改

 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=部分中的URL中,我更改了实名的数字。

我想像是从seller_name=更改为第一个,并且从seller_name看到它。

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

4 个答案:

答案 0 :(得分:1)

使用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)

答案 1 :(得分:0)

xyplot(y1 + y2 ~ x,ylab = "y1 and y2", type = "l", auto.key = list(points = F,lines = T), par.settings = list(superpose.line = list(col = c("red","green"))))

尝试这个:

答案 2 :(得分:0)

此解决方案不采用查询参数的顺序或您要替换的ID的长度。它仅假设查询是用&分隔的,并且存在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)

答案 3 :(得分:0)

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

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