I want to count all times a value occurred up to the present position in pandas/python. A condition must be accounted for (numbers must be present in 'score' column to count a game as played; shows up as NaN if no value in excel file I read from).
Code below is where i'm at:
import pandas as pd
df = pd.read_excel('G:\Project\SOQ1.xlsx')
df['date'] = pd.to_datetime(df['date'])
df = df.sort(columns='date')
df = df.set_index('date')
def calc_all_count(team_name):
home_count = df['home'].value_counts().get(team_name, 0)
away_count = df['away'].value_counts().get(team_name, 0)
all_count = home_count + away_count
return all_count
def calc_home_count(team_name):
home_count = df['home'].value_counts().get(team_name, 0)
return home_count
def calc_away_count(team_name):
away_count = df['away'].value_counts().get(team_name, 0)
return away_count
df['hag'] = df['home'].map(calc_all_count)
df['aag'] = df['away'].map(calc_all_count)
df['hahg'] = df['home'].map(calc_home_count)
df['aaag'] = df['away'].map(calc_away_count)
print df
league home away hscore ascore hag aag hahg aaag
date
2015-01-03 03:02:00 MLB Cle Tex 9 6 3 15 2 7
2015-05-10 03:03:00 MLB Bos Cle 6 7 16 3 7 1
2015-10-15 03:00:00 MLB Tex Bos 5 2 15 16 8 9
2015-10-15 03:30:00 MLB Tex Bos 1 6 15 16 8 9
2015-10-16 00:00:00 MLB Tex Bos 4 4 15 16 8 9
2015-10-17 03:30:00 MLB Bos Tex 2 8 16 15 7 7
2015-10-18 00:00:00 MLB Tex Bos 9 10 15 16 8 9
2015-10-20 00:00:00 MLB Bos Tex 2 3 16 15 7 7
2015-10-21 00:00:00 MLB Tex Bos 5 1 15 16 8 9
2015-10-22 03:00:00 MLB Tex Bos 5 3 15 16 8 9
2015-10-23 00:00:00 MLB Bos Tex 3 4 16 15 7 7
2015-10-25 23:00:00 MLB Bos Tex 6 6 16 15 7 7
2015-10-25 23:00:00 MLB Bos Tex 5 1 16 15 7 7
2015-10-26 00:00:00 MLB Tex Bos 9 6 15 16 8 9
2015-10-27 01:30:00 MLB Bos Tex 10 5 16 15 7 7
2015-10-28 01:00:00 MLB Tex Bos NaN NaN 15 16 8 9
2015-11-20 03:01:00 MLB Cle Bos NaN NaN 3 16 2 9
What I want is for the no of games played prior to each game. So first game/row should read 0 for all numbers as nobodys played yet. Should look like this:
league home away hscore ascore hag aag hahg aaag
date
2015-01-03 03:02:00 MLB Cle Tex 9 6 0 0 0 0
2015-05-10 03:03:00 MLB Bos Cle 6 7 0 1 0 0
2015-10-15 03:00:00 MLB Tex Bos 5 2 1 1 0 0
2015-10-15 03:30:00 MLB Tex Bos 1 6 2 2 1 1
2015-10-16 00:00:00 MLB Tex Bos 4 4 3 3 2 2
2015-10-17 03:30:00 MLB Bos Tex 2 8 4 4 1 1
2015-10-18 00:00:00 MLB Tex Bos 9 10 5 5 3 3
2015-10-20 00:00:00 MLB Bos Tex 2 3 6 6 2 2
2015-10-21 00:00:00 MLB Tex Bos 5 1 7 7 4 4
2015-10-22 03:00:00 MLB Tex Bos 5 3 8 8 5 5
2015-10-23 00:00:00 MLB Bos Tex 3 4 9 9 3 3
2015-10-25 23:00:00 MLB Bos Tex 6 6 10 10 4 4
2015-10-25 23:00:00 MLB Bos Tex 5 1 11 11 5 5
2015-10-26 00:00:00 MLB Tex Bos 9 6 12 12 6 6
2015-10-27 01:30:00 MLB Bos Tex 10 5 13 13 6 6
2015-10-28 01:00:00 MLB Tex Bos NaN NaN 14 14 7 7
2015-11-20 03:01:00 MLB Cle Bos NaN NaN 2 14 1 7
How can I structure to count 'before' present position? I am thinking I should be using .iloc or .ix but I can't figure it out.
Any help on achieving this or better code appreciated. Tips on asking the question better appreciated too.