Python Pandas - Merge based on substring in string

前端 未结 2 1797
南方客
南方客 2020-12-06 07:41

I have 2 dataframes with the following format:

df_search

SEARCH
part1
anotherpart
onemorepart


df_all

FILE             EXTENSION    PATH
part1_1         .         


        
相关标签:
2条回答
  • 2020-12-06 07:55

    Use str.extract + insert:

    pat = "|".join(df_search.SEARCH)
    df_all.insert(0, 'SEARCH', df_all['FILE'].str.extract("(" + pat + ')', expand=False))
    print (df_all)
             SEARCH           FILE EXTENSION                            PATH
    0         part1        part1_1      .prt        //server/folder1/part1_1
    1         part1        part1_2      .prt        //server/folder2/part1_2
    2         part1        part1_2      .pdf        //server/folder3/part1_2
    3         part1        part1_3      .prt        //server/folder2/part1_3
    4   anotherpart  anotherpart_1      .prt  //server/folder1/anotherpart_1
    5   anotherpart  anotherpart_2      .prt  //server/folder3/anotherpart_2
    6   anotherpart  anotherpart_3      .prt  //server/folder2/anotherpart_3
    7   anotherpart  anotherpart_3      .cgm  //server/folder1/anotherpart_3
    8   anotherpart  anotherpart_4      .prt  //server/folder3/anotherpart_4
    9   onemorepart  onemorepart_1      .prt  //server/folder2/onemorepart_1
    10  onemorepart  onemorepart_2      .prt  //server/folder1/onemorepart_2
    11  onemorepart  onemorepart_2      .dwg  //server/folder2/onemorepart_2
    12  onemorepart  onemorepart_3      .prt  //server/folder1/onemorepart_3
    13  onemorepart  onemorepart_4      .prt  //server/folder1/onemorepart_4
    
    0 讨论(0)
  • 2020-12-06 08:05

    I will do it in this way:

    df_all['SEARCH'] = ''
    for val in df_search.SEARCH:
        df_all.loc[df_all['FILE'].str.match(val), 'SEARCH'] = val
    
    0 讨论(0)
提交回复
热议问题