df: make country other_columns salehonda tokyo data 1honda hirosima data 0toyota tokyo data 1toyota hirosima data 0suzuki tokyo data 0suzuki hirosima data 0ferrari tokyo data 1ferrari hirosima data 0nissan tokyo data 1nissan hirosima data 0条件:保持具有1,0的货币对期望的输出make country other_columns salehonda tokyo data 1honda hirosima data 0toyota tokyo data 1toyota hirosima data 0ferrari tokyo data 1ferrari hirosima data 0nissan tokyo data 1nissan hirosima data 0我尝试了什么(这显然不起作用)outdf = df.groupby('sale').filter(lambda x: x > 0)我应该将两个列都分组吗(“ sale”和“ make”)?
2 回答
慕无忌1623718
TA贡献1744条经验 获得超4个赞
这是使用groupby+的解决方案set。这应该是可扩展的,不需要订购:
s = df.groupby('make')['sale'].apply(set)
res = df[df['make'].map(s) >= {0, 1}]
print(res)
make country other_columns sale
0 honda tokyo data 1
1 honda hirosima data 0
2 toyota tokyo data 1
3 toyota hirosima data 0
6 ferrari tokyo data 1
7 ferrari hirosima data 0
8 nissan tokyo data 1
9 nissan hirosima data 0
添加回答
举报
0/150
提交
取消