1

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   

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   

1 Answer 1

2

You can use 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

Another solution by using 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
Sign up to request clarification or add additional context in comments.

3 Comments

Wen, TY for the quick response...can you possibly explain the mechanics of the code? df.groupby(['Date','R']).filter(lambda x : (x['Win']>=10).any())
@Derek it group by Date and R , find any value no less that 10 for all Win within the group, if any of them match , keep all , else drop it
@Derek if this is what you need , can you consider accept it ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.