DF1:
NAME LIST_1
BILL ['ZF1', 'ZF2', 'ZF3', 'ZF9', 'ZF11']
PAUL ['ZF1', 'ZF4', 'ZF5', 'ZF2', 'ZF3']
JOHN ['ZF1', 'ZF2', 'ZF5', 'ZF6']
DF2:
ID LIST_2
ZB1 ['ZF1', 'ZF2', 'ZF3']
ZB2 ['ZF1', 'ZF4', 'ZF5']
ZB3 ['ZF2', 'ZF5', 'ZF6']
NEEDED RESULT:
DF1 (Can also be a new DF):
NAME LIST_1 MATCH
BILL ['ZF1', 'ZF2', 'ZF3', 'ZF9', 'ZF11'] ['ZB1']
PAUL ['ZF1', 'ZF4', 'ZF5', 'ZF2', 'ZF3'] ['ZB1', 'ZB2']
JOHN ['ZF1', 'ZF2', 'ZF5', 'ZF6'] ['ZB3']
I have not really tried much of anything yet as I am confused by the list comparisons. I expect I will need to explode DF1 and DF2 and compare and use merge? Any help would be appreciated.
>Solution :
Try:
df1['MATCH'] = df1.apply(lambda x: [i for i, l in zip(df2.ID, df2.LIST_2) if all(v in x['LIST_1'] for v in l)] , axis=1)
print(df1)
Prints:
NAME LIST_1 MATCH
0 BILL [ZF1, ZF2, ZF3, ZF9, ZF11] [ZB1]
1 PAUL [ZF1, ZF4, ZF5, ZF2, ZF3] [ZB1, ZB2]
2 JOHN [ZF1, ZF2, ZF5, ZF6] [ZB3]
Optionally: If values in LIST_1/LIST_2 columns are strings, convert them to lists:
from ast import literal_eval
df1.LIST_1 = df1.LIST_1.apply(literal_eval)
df2.LIST_2 = df2.LIST_2.apply(literal_eval)