NFeruch - FreePalestine
NFeruch - FreePalestine

Reputation: 1154

pandas calculations on a groupby

using the dataframe code at this link https://pastebin.com/d1aW7u5N

I grouped it like this df.groupby([col, 'win']).count() to achieve this output desc

But I want to have it be for every death, display the percentage of win/(win+loss) (basically show the win percentage) using the match id

For example, the end dataframe would look as such:

(deaths, percentage),  
(0, 1) because 9 wins and 0 losses for deaths=0  
(1, .77) because 7 wins and 2 losses for deaths=1 and 7/(7+2) = .77  
(2, .84) because 21 wins and 4 losses for deaths=2 and 21/(21+4) = .84  
(3, .74) ...  

Problem: I want to display the winrates per deaths as outlined above, thanks

Upvotes: 0

Views: 33

Answers (1)

NFeruch - FreePalestine
NFeruch - FreePalestine

Reputation: 1154

This was my final answer that allowed me to see the winrates (percentage within each group) for all values

df = df.groupby([col, 'win']).count().reset_index().pivot('deaths', 'win', 'matchID').fillna(0)
df['winrate'] = (df[1] / (df[1] + df[0]))
df.columns.name = None
df = df.drop([0, 1], axis=1)
df

Upvotes: 1

Related Questions