简体   繁体   中英

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 描述

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

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')
df[0] = np.nan_to_num(df[0])
df[1] = np.nan_to_num(df[1])
df['winrate'] = (df[1] / (df[1] + df[0]))
df.columns.name = None
df = df.drop([0, 1], axis=1)
df

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM