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

Python: Finding individuals with conditions from two lists using Python (pandas)

I have the following dataframe:

df = 
id medication
1  A
1  B
1  A
2  Z
2  A
2  A
3  B
3  D 
3  A

I create two lists of medications:

ListA = ['A', 'Z']
ListB = ['B', 'C']

I want to obtain those individuals that have any of the medications from listA and any of the medications from listB so that the answer is:

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

dfOutput = 
id medication
1  A
1  B
3  B
3  A

So far I am trying the following:

dfOutput = df.groupby("id").filter(lambda x : pd.Series([*ListA,*ListB]).isin(x['medication']).all())

>Solution :

(df.groupby('id')['medication']
  .agg(result = lambda x: x.isin(ListA).any() and x.isin(ListB).any())
  .reset_index().merge(df)
  .loc[lambda x: x['medication'].isin(ListA+ListB)*x.result].drop_duplicates())

   id  result medication
0   1    True          A
1   1    True          B
6   3    True          B
8   3    True          A
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