Python/Pandas:按过滤条件划分的数据框子集 [英] Python / Pandas: Dataframe subset by filter criteria

查看:92
本文介绍了Python/Pandas:按过滤条件划分的数据框子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框,它是比赛和结果的列表.

I have the following dataframe which is list of races and results.

      Date       R   #   Fin  Win
0     11182017   1   1   2     0 
1     11182017   1   2   1     5   
2     11182017   1   3   3     0   
3     11182017   2   1   2     0   
4     11182017   2   2   1     10   
5     11182017   3   1   1     6    
6     11182017   3   2   2     0   

我只想返回赢列大于或等于10的比赛(所有参赛者不仅是10的行).

I want to only return the races (all entrants not just the row with 10) where the Win column is greater than or equal to 10. Example below

      Date       R   #   Fin  Win
3     11182017   2   1   2     0   
4     11182017   2   2   1     10   

推荐答案

您可以使用 groupby + filter

df.groupby(['Date','R']).filter(lambda x : (x['Win']>=10).any())
Out[568]: 
       Date  R  #  Fin  Win
3  11182017  2  1    2    0
4  11182017  2  2    1   10

使用 transform

df[df.groupby(['Date','R']).Win.transform(lambda x : (x>=10).any())]
Out[573]: 
       Date  R  #  Fin  Win
3  11182017  2  1    2    0
4  11182017  2  2    1   10

这篇关于Python/Pandas:按过滤条件划分的数据框子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆