Skip to content

Instantly share code, notes, and snippets.

@jes-moore
Created December 22, 2018 19:28
Show Gist options
  • Save jes-moore/f70114339e47e44c91de44fee4ced726 to your computer and use it in GitHub Desktop.
Save jes-moore/f70114339e47e44c91de44fee4ced726 to your computer and use it in GitHub Desktop.
def agg_rebounds_df2():
## Aggregate shots and goals
shot_df = load_rebounds_df()
shot_df_agg2 = shot_df.groupby(['shotTimeDiff', 'nextShotResult']).agg({
'counter':
'sum'
}).reset_index()
## Create pivot table
shot_df_agg2 = shot_df_agg2.pivot_table(
aggfunc='sum',
columns=['nextShotResult'],
fill_value=0,
index=['shotTimeDiff']).reset_index()
## Drop multilevel and add ratio
shot_df_agg2.columns = shot_df_agg2.columns.droplevel()
shot_df_agg2.columns = ['shotTimeDiff', 'numGoals', 'numShots']
shot_df_agg2['goalRatio'] = shot_df_agg2['numGoals'] / (
shot_df_agg2['numGoals'] + shot_df_agg2['numShots'])
return shot_df_agg2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment