Skip to content

Instantly share code, notes, and snippets.

@databyjp
Last active March 17, 2022 12:33
Show Gist options
  • Save databyjp/a0177a7225d5feda5bc612950809c7c8 to your computer and use it in GitHub Desktop.
Save databyjp/a0177a7225d5feda5bc612950809c7c8 to your computer and use it in GitHub Desktop.
# ========================================
# Now let's do the same for each team
# ========================================
shots_df["teamId"] = shots_df["teamId"].astype(int)
gdf_list = list()
for tm_id in shots_df.teamId.unique():
tm_df = shots_df[shots_df.teamId == tm_id]
tm_gdf = tm_df.groupby("shot_zone").agg({"shot_made": "sum", "period": "count"})
tm_gdf = tm_gdf.reset_index().rename({"period": "shot_atts"}, axis=1)
tm_name = teams.find_team_name_by_id(tm_id)["abbreviation"]
tm_gdf = tm_gdf.assign(team=tm_name)
tm_gdf = tm_gdf.assign(shot_freq=tm_gdf.shot_atts / tm_gdf.shot_atts.sum())
tm_gdf = tm_gdf.assign(shot_acc=tm_gdf.shot_made / tm_gdf.shot_atts)
tm_gdf = tm_gdf.assign(rel_freq=tm_gdf.shot_freq - gdf.shot_freq)
tm_gdf = tm_gdf.assign(rel_acc=tm_gdf.shot_acc - gdf.shot_acc)
tm_gdf = tm_gdf.assign(rel_ev=tm_gdf.rel_acc * 2)
tm_gdf.loc[tm_gdf["shot_zone"].str.contains("3pt"), "rel_ev"] = tm_gdf[tm_gdf["shot_zone"].str.contains("3pt")]["rel_acc"] * 3
gdf_list.append(tm_gdf)
gdf_tot = pd.concat(gdf_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment