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))\ + '
Scoring Ratio = ' + shot_df_agg['goalRatio'].apply(lambda x:"{0:.2f}%".format(x * 100))\ + '
Number of Shots = ' + shot_df_agg['numShots'].apply(lambda x: str(x))\ + '
Number of Goals = ' + shot_df_agg['numGoals'].apply(lambda x: str(x)) return shot_df_agg