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:
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