Skip to content

Instantly share code, notes, and snippets.

@jes-moore
Created December 22, 2018 19:29
Show Gist options
  • Save jes-moore/704d1a1888920ae35e4b358c136708a4 to your computer and use it in GitHub Desktop.
Save jes-moore/704d1a1888920ae35e4b358c136708a4 to your computer and use it in GitHub Desktop.
def get_seasonal_reb_df():
shot_to_reb = load_rebounds_df()
## Use only regular season
shot_to_reb = shot_to_reb[shot_to_reb.type == 'R']
rebound_df = shot_to_reb.groupby(['for_name', 'season', 'lead_to_reb']).agg({
'counter':
'count'
}).reset_index()
rebound_df = rebound_df.pivot_table(
index=['season', 'for_name'], columns='lead_to_reb').reset_index()
rebound_df.columns = rebound_df.columns.droplevel()
rebound_df.columns = ['season', 'team_name', 'no_rebound', 'rebound']
rebound_df['total_shots'] = rebound_df['no_rebound'] + rebound_df['rebound']
rebound_df['rebound_ratio'] = rebound_df['rebound'] / rebound_df['total_shots']
## Remove 2012 (only half a season)
rebound_df = rebound_df[rebound_df.season != '2012']
## Get seasonal data and merge
seasonal_df = get_seasonal_summary('R')
seasonal_df = rebound_df.merge(
seasonal_df,
right_on=['season', 'combined_name'],
left_on=['season', 'team_name'])
#Remove season-team pairs with far too low shot count in the play_df
seasonal_df = seasonal_df[seasonal_df.total_shots > seasonal_df.shots * 0.75]
return seasonal_df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment