Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tricky aggregation using pandas

Tags:

python

pandas

Am new to pandas and curious if this is too tricky to accomplish using it.

Example input:

time       person   game_id   won
-----------------------------------
12:34:01   John     3         False
12:34:04   Ringo    2         True
12:35:05   John     3         False
12:36:01   John     3         True
12:36:12   Ringo    3         True
12:36:41   Paul     4         False
12:37:01   George   2         False
12:37:41   George   2         False

It shows a number of persons playing a number of games over time. The won column indicates if the person won or not at that time.

What I want as output is how many people won each game at least once. But also how many people played the game and never won.

Example Output:

game_id   won     count
-----------------------
2         True    1
          False   1
3         True    2
          False   0
4         True    0
          False   1
like image 979
Cody Norden Avatar asked Mar 09 '26 14:03

Cody Norden


1 Answers

More or less what @DSM was saying:

In [3]: grouped = df.groupby('game_id')

In [4]: won = grouped.won.sum()

In [5]: DataFrame({True: won, False: grouped.person.nunique() - won}).stack()
Out[5]: 
game_id       
2        False    1
         True     1
3        False    0
         True     2
4        False    1
         True     0
dtype: float64
like image 78
Chang She Avatar answered Mar 11 '26 20:03

Chang She



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!