I have a drug reference as a df called df_drug_ref (below). There are three drugs (A, B, and C). The corresponding ATC is listed in the second column. However, if patient has a DIN within the Drug_BIN_Id_Exclusion list, then s/he would not be considered as using that drug (ie. 011235 for Drug A).
Drug Drug_ATC_Id Drug_DIN_Id_Exclusion
A N123 [011235]
B B5234 [65413, 654351]
C N32456 []
The following is the other df called df_row. This captures all the drugs dispensed by each individual. And each individual has his own People_Id.
People_Id Drug_ATC Drug_DIN A B C
1001 N123
1001 N123 011235
1001 N32456 011232
1001 N111
1002 B5234 65413
1002 B5234 654090
1002 N123 011235
I would like to assign '1' for the corresponding drug (looping iteratively to check for A, B, or C and assigning to the corresponding columns) if, in that row, the ATC code matches with the drug reference and the DIN is not contained within the exclusion list. The result should be:
People_Id Drug_ATC Drug_DIN A B C
1001 N123 1 0 0
1001 N123 011235 0 0 0
1001 N32456 011232 0 0 1
1001 N111 0 0 0
1002 B5234 65413 0 0 0
1002 B5234 654090 0 1 0
1002 N123 011235 0 0 0
I understand how to use apply function within the same df itself, but I don't know how to also use an external df as reference.