从数据框中根据多列条件获取行。

huangapple go评论69阅读模式
英文:

Get rows from dataframe with multicolumn conditions

问题

以下是翻译好的部分:

将很容易展示我想要的,然后解释它。考虑以下数据框:

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

期望的输出是:

    mac     s
0   1      aa 
4   1      ee

我需要找到所有“mac”值,其中既有“aa”值又有“ee”值。在上面的示例中,mac = 1 是满足条件的。而对于mac = 3,它不满足条件,因为有“aa”值但没有“ee”值。

英文:

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'.

答案1

得分: 7

Sure, here's the translated code part:

IIUC 使用 `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
英文:

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

huangapple
  • 本文由 发表于 2020年1月3日 23:52:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定