Regex to compare and extract alphabet characters using python

前端 未结 2 762
旧巷少年郎
旧巷少年郎 2021-01-22 08:11

Hi I have a dataset as shown below:

Format,Message,time
A,ab@1 yl@5 rd@20 pp@40,3
B,bc@1 gn@7 yl@20 ss@25 rd@50, 21
C,cc@1 yl@9 rd@20, 22

I wou

2条回答
  •  没有蜡笔的小新
    2021-01-22 08:58

    I think, this can be done with built in string functions. Try this!

    def f(mess):
        p1 = mess.find('yl')
        p2 = mess.find('rd')
        return int(mess[p1+3:].split(' ')[0]),int(mess[p2+3:].split(' ')[0])
    
    df['vals'] =df['Message'].apply(f) 
    
    df['status'] = df.apply(lambda row:  'g' if min(row['vals']) > row.time \
                            else 'y' if row.vals[1]>row.time  \
                            else 'r', axis=1)
    
    print(df)
    

    output:

      Format                  Message  time      vals status
    0      A    ab@1 yl@5 rd@20 pp@40     3   (5, 20)      g
    1      B  bc@1  yl@20 ss@25 rd@50    21  (20, 50)      y
    2      C          cc@1 yl@9 rd@20    22   (9, 20)      r
    

提交回复
热议问题