Home > Net >  Pandas calculate the winrate for particular group
Pandas calculate the winrate for particular group

Time:07-17

I have this table enter image description here

I want to create new column with winrate of every team, how can i do this?

CodePudding user response:

I'm assuming you have this data in a DataFrame.

import pandas as pd

# some data in a dict
data = [{'team': 'BOOM Esports', 'IS_WIN': False, 'count': 9},
        {'team': 'BOOM Esports', 'IS_WIN': True, 'count': 5},
        {'team': 'BetBoom Team', 'IS_WIN': False, 'count': 7},
        {'team': 'BetBoom Team', 'IS_WIN': True, 'count': 9}]

# creating dataframe with dict
df = pd.DataFrame(data)

You could work with df.groupby:

matches_won = df[df['IS_WIN'] == True].groupby('team')['count'].sum()
matches = df.groupby('team')['count'].sum()

winning_rate = matches_won / matches

Output:

print(winning_rate)

# team
# BOOM Esports    0.357143
# BetBoom Team    0.562500
# Name: count, dtype: float64
  • Related