Cole Ragone
Cole Ragone

Reputation: 47

Iterating through pandas dataframe and appending a dictionary?

I am trying to transition from excel to python, and for practice I would like to analyze sports data from the NFL season. I have created a pandas dataframe with the data I would like to track, but I was wondering how I can go through the data and create a dictionary with each teams wins and loses. I thought that I could iterate through the dataframe and check whether or not each team has already been entered into the dictionary, and if not append their name to it.

Any advice?

closing_lines dataframe sample:

Year Week side type line odds outcome
0 2006 01 PIT MONEYLINE NaN -125.0 1.0
1 2006 01 MIA MONEYLINE NaN 105.0 0.0
2 2006 01 MIA SPREAD 1.5 NaN 0.0
3 2006 01 PIT SPREAD -1.5 NaN 1.0
results = {'Team': [], 'Wins': [], 'Losses': []}

# iterate through the data
# check to see if the dictionary has the team we are looking at 
# if it doesn't, add it to the dictionary
# if it does, add a unit to either the wins or the losses 

closing_lines = closing_lines.reset_index() #make sure that the index matches the number of rows
for index, row in closing_lines.iterrows(): 
    for key, Team in results.items(): 
        if Team == closing_lines[row, 'side']:
            pass
        else:
            results['Team'].append(closing_lines[row, 'side'])

Upvotes: 1

Views: 60

Answers (1)

Jeff
Jeff

Reputation: 1264

The more pandas way of doing this is to create a new data frame indexed by team with columns for wins and losses. The groupby method can help with this. You can group the rows of your dataframe by team and then run some kind of summary over the results, e.g.:

closing_lines.groupby('side')['outcome'].sum()

creates a new Series indexed by 'side' with the sum of the 'outcome' column for each 'side' (which I think is Wins for this data).

Check out this answer to see how to count zeros and non-zeros in a groupby column.

Upvotes: 2

Related Questions