Extract string from a dataframe comparing to a list

前端 未结 1 633

I am trying to extract strings from a DF in pandas dataframe and the source strings are in a list from which I have to match. I tried using a df.str.extract(list1)

相关标签:
1条回答
  • 2020-12-01 23:33

    You can use extract with join all values in List by | what means or in regex:

    List1 = ['Three has' , 'Mail' , 'Done' , 'Game' , 'Time has come']
    df['Col 3'] = df['Col 2'].str.extract("(" + "|".join(List1) +")", expand=False)
    print (df)
       Col 1           Col 2      Col 3
    0      1        The date        NaN
    1      2  Three has come  Three has
    2      3       Mail Sent       Mail
    3      4       Done Deal       Done
    

    Another solution:

    List1 = ['Three has' , 'Mail' , 'Done' , 'Game' , 'Time has come']
    
    df['Col 3'] = df['Col 2'].apply(lambda x: ''.join([L for L in List1 if L in x]))
    df['Col 3'] = df['Col 3'].mask(df['Col 3'] == '')
    print (df)
       Col 1           Col 2      Col 3
    0      1        The date        NaN
    1      2  Three has come  Three has
    2      3       Mail Sent       Mail
    3      4       Done Deal       Done
    
    0 讨论(0)
提交回复
热议问题