Created
December 22, 2018 19:28
-
-
Save jes-moore/f70114339e47e44c91de44fee4ced726 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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