英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论