Demonking28
Demonking28

Reputation: 749

Adding Specific Team Score in pandas

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use("fivethirtyeight")

df_2010=pd.read_csv("c:/users/ashub/downloads/documents/MLB 2010.csv",index_col=0)
df_new=df_2010[["Home Score","Away Score","Home Team","Away Team","Home Hits","Away Hits","Home Err","Away Err"]]
#print(df_2010)

flag=df_2010["Home Score"]>df_2010["Away Score"]
df_new["Home Score Index"]= flag.astype(int)
df_new["Away Score Index"]= (~flag).astype(int)

flag1=df_2010["Home Score"]/df_2010["Home Hits"]
df_new["Home to Hits Index"]= flag1.astype(float)

flag1=df_2010["Away Score"]/df_2010["Away Hits"]
df_new["Away to Hits Index"]= flag1.astype(float)

flag1=df_2010["Home Err"]/df_2010["Home Score"]
df_new["Home Error Factor"]= flag1.astype(float)

flag1=df_2010["Away Err"]/df_2010["Away Score"]
df_new["Away Error Factor"]= flag1.astype(float)

df_new["Home Error Factor"].fillna(0,inplace=True)
df_new["Away Error Factor"].fillna(0,inplace=True)


wins_home=sum(df_new["Home Score Index"].tolist())
total_games=len(df_2010)
prob_win_at_home=wins_home/total_games
prob_lose_at_home=1-prob_win_at_home
print(prob_win_at_home)
print(prob_lose_at_home)


df_new.to_html("c:/users/ashub/desktop/ashu.html")

Sample Data

Now i want to calculate the no of wins of a particular team when they played at home out of the total no of games they played at home also i want to calculate the no of wins of away teams as well.How to approach this?

Upvotes: 0

Views: 381

Answers (2)

asongtoruin
asongtoruin

Reputation: 10359

When you groupby you can aggregate in multiple ways simultaneously. Using erocoar's example dataframe:

import pandas as pd

games = pd.DataFrame(data={'Home': ['A', 'B', 'A', 'A', 'B'],
                           'Away': ['B', 'C', 'C', 'B', 'A'],
                           'Home Score': [0, 1, 4, 3, 0],
                           'Away Score': [1, 2, 2, 1, 1]})

games['Home Win'] = (games['Home Score'] > games['Away Score']).astype(int)

summary = games.groupby('Home').agg({'Home Win': 'sum',
                                     'Home': 'count'})

summary['Home Win Ratio'] = summary['Home Win'] / summary['Home']

Will give you the output:

      Home Win  Home  Home Win Ratio
Home                                
A            2     3        0.666667
B            0     2        0.000000

Upvotes: 2

erocoar
erocoar

Reputation: 5893

This would be one approach I think ?

import pandas as pd

games = pd.DataFrame(data = {"home" : ["A", "B", "A", "A", "B"],
                             "away" : ["B", "C", "C", "B", "A"],
                             "homescore" : [0, 1, 4, 3, 0],
                             "awayscore" : [1, 2, 2, 1, 1]})

games["homewin"] = games.apply(lambda row: 1 if row.homescore > row.awayscore else 0, axis=1)

g = games.groupby(by=["home", "homewin"]).size().reset_index(name="games")
g["homewin"] = g.apply(lambda row: row.homewin*row.games, axis=1)
g = g.groupby(by=["home"]).sum()
g["homewinratio"] = g["homewin"]/g["games"]

    g
    Out[105]: 
          homewin  games  homewinratio
    home                              
    A           2      3      0.666667
    B           0      2      0.000000

Although I'm sure there are much better ones - I'm curious too

Upvotes: 2

Related Questions