Last active
December 22, 2018 19:23
-
-
Save jes-moore/9581307e763d97f05a0febe9e5ed2ef7 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_df(): | |
shot_df = load_rebounds_df() | |
# Aggregate by Time Differential | |
shot_df = shot_df[shot_df.shotTimeDiff > 0] | |
shot_df_agg = shot_df.groupby('shotTimeDiff').agg({'counter':'sum'}) | |
# Aggregate shots and goals | |
shot_df_agg = shot_df.groupby(['shotTimeDiff','nextShotResult'])\ | |
.agg({'counter':'sum'}).reset_index() | |
# Create pivot table | |
shot_df_agg = shot_df_agg.pivot_table(aggfunc='sum', | |
columns=['nextShotResult'], | |
fill_value=0, | |
index=['shotTimeDiff']).reset_index() | |
# Drop multilevel and add ratio | |
shot_df_agg.columns = shot_df_agg.columns.droplevel() | |
shot_df_agg.columns = ['shotTimeDiff','numGoals','numShots'] | |
shot_df_agg['goalRatio'] = shot_df_agg['numGoals'] /\ | |
(shot_df_agg['numGoals'] + shot_df_agg['numShots']) | |
#Set Minimum Shots for Differential | |
shot_df_agg = shot_df_agg[shot_df_agg.numShots > 1500] | |
shot_df_agg['goalRatioText'] ='Time b/w Rebound = ' \ | |
+ shot_df_agg['shotTimeDiff'].apply(lambda x: str(x))\ | |
+ ' <br>Scoring Ratio = ' + shot_df_agg['goalRatio'].apply(lambda x:"{0:.2f}%".format(x * 100))\ | |
+ ' <br>Number of Shots = ' + shot_df_agg['numShots'].apply(lambda x: str(x))\ | |
+ ' <br>Number of Goals = ' + shot_df_agg['numGoals'].apply(lambda x: str(x)) | |
return shot_df_agg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment