【发布时间】:2018-12-18 09:19:18
【问题描述】:
我有如下数据:
Format,Message,time
A,gn@2 ab@1 yl@5 rd@20 pp@40,3
B,w:w23w4w5w6w7gn@3 gn@7 yl@20 ss@25 rd@50,21
C,cc@1 fgn@4 yl@9 rd@20,22
D,rg@1 fedsf@5 rww@10 yl@20 rd@26,30
我的预期结果是提取 gn,yl 和 rd 之后的数字
Format,Message,time,gn,yl,rd
A,gn@2 ab@1 yl@5 rd@20 pp@40,3,2,5,20
B,w:w23w4w5w6w7gn@3 an@7 yl@20 ss@25 rd@50,21,3,20,50
C,cc@1 fgn@4 yl@9 rd@20,22,4,9,20
D,rg@1 fedsf@5 rww@10 yl@20 rd@26,30,0,20,26
到目前为止,我能够获得 yl 和 rd,但我无法提取 gn 之后的数字。请注意,gn 元素可能在 gn 之前包含一些其他字符,并且在 gn@ 之后需要数字
def f(mess):
p1 = mess.find('yl')
p2 = mess.find('rd')
b = mess[p1+3:].split(' ')[0]
c = mess[p2+3:].split(' ')[0]
return int(b),int(c)
id['vals'] = id['Message'].apply(f) #with this im able to get the numbers from yl and rd
【问题讨论】:
-
我喜欢你的用户名
-
对 gn 使用相同的逻辑和 p3 变量有什么问题?
-
我尝试使用 p3,但由于 gn 元素的长度不同,它没有提取数字并最终提取了该值中的一些其他字符。我将更新我的问题,因为我的实际数据中的值要长得多。我缩短并更改了数据以尝试和咨询,以便我能理解逻辑
-
@SamMason 这比链接中提出的提取更多元素的问题更进一步