Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Populate new column in DF1 if List in DF2 is in List in DF1. Can be multiple updates in DF1, so need the results to be in a list

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading