Roman Kazmin
Roman Kazmin

Reputation: 981

Get rows from dataframe with multicolumn conditions

It will be easy to show what I want then explain it. Consider the following dataframe:

dr = {'mac':[1, 3, 2, 4, 1, 2], 's': ['aa', 'aa', 'c', 'd', 'ee', 'f']}
d = pd.DataFrame(data=dr)

Desiable output is:

    mac     s
0   1      aa 
4   1      ee

I need to find all 'mac' for which there are both 'aa' and 'ee' values. In example about it is true for mac = 1. For mac = 3 it is false because there is 'aa' value but not 'ee'.

Upvotes: 3

Views: 64

Answers (1)

BENY
BENY

Reputation: 323376

IIUC filter with isin

out=d.groupby('mac').filter(lambda x : pd.Series(['aa','ee']).isin(x['s']).all())
Out[62]: 
   mac   s
0    1  aa
4    1  ee

Upvotes: 7

Related Questions