2

I have a dataframe that roughly looks like this:
enter image description here

I could group and sort them with df_players.groupby(['vehicle']).agg({'frags':sum}).sort_values(['frags'],ascending=False)
enter image description here
How can I group the vehicles and get their win rates just like this:
enter image description here

EDIT: (data)

[ {"player": "bob", "vehicle": "a", "hasWon": True, "frags": 5}, {"player": "foo", "vehicle": "b", "hasWon": False, "frags": 3}, {"player": "bar", "vehicle": "c", "hasWon": True, "frags": 2} ] 
1
  • kindly post data, not pics Commented May 14, 2020 at 7:28

3 Answers 3

1

I think you need aggregate mean of hasWon column, because Trues are processing like 1, then multiple by 100, round and last convert to column:

df_players = pd.DataFrame({'vehicle': list('aabbccc'), 'hasWon':[True, False, True, True, False, True, False], 'frags':[5,2,3,6,5,4,5]}) print (df_players) vehicle hasWon frags 0 a True 5 1 a False 2 2 b True 3 3 b True 6 4 c False 5 5 c True 4 6 c False 5 df = (df_players.groupby(['vehicle'])['hasWon'] .mean() .mul(100) .round(2) .reset_index(name='winrate')) print (df) vehicle winrate 0 a 50.00 1 b 100.00 2 c 33.33 
Sign up to request clarification or add additional context in comments.

5 Comments

How would I ignore vehicles that has only n amount of entries?
@conquistador - so in data sample if n=3 are removed vehicles of c, because 3 times? and if some another group e.g. d has 4 values, not removed?
Yes. How would I filter it?
@conquistador So before my solution with mean use df_players = df_players[df_players['vehicle'].map( df_players['vehicle'].value_counts()).ne(3)]
@conquistador - Solution from this
1

I would just assign a new column to just use a mean:

df.assign(winrate=np.where(df['hasWon'], 100, 0)).groupby('vehicle').agg({'winrate': 'mean'}) 

Comments

1

Solution

# Assuming # winrate = sum(hasWon*frags)/sum(frags) # df['winrate'] = df['hasWon']*df['frags']/df['frags'].sum() # Calculate Aggregate Result result = (df.groupby(['vehicle']) .agg({ 'frags': sum, 'winrate': sum }) 

enter image description here

Dummy Data

import numpy as np import pandas as pd df = pd.DataFrame({'player': ['bob', 'foo', 'bar', 'foo2', 'bar2'], 'vehicle': list('abcab'), 'hasWon': [True, False, True, True, True], 'frags': [5, 3, 2, 4, 2]}) # Assuming # winrate = sum(hasWon*frags)/sum(frags) df['winrate'] = df['hasWon']*df['frags']/df['frags'].sum() df 

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.