Skip to content

Instantly share code, notes, and snippets.

@databyjp
Created March 17, 2022 13:57
Show Gist options
  • Save databyjp/efd0180f59901f7eb0012d2fea96d4cc to your computer and use it in GitHub Desktop.
Save databyjp/efd0180f59901f7eb0012d2fea96d4cc to your computer and use it in GitHub Desktop.
# Build more granular shot bins
filt_start = 0
filt_width = 2
filt_inc = 0.25
gdf_list = list()
for i in range(1 + int(30 * (1 / filt_inc))):
filt_df = shots_df[(shots_df["shotDistance"] >= filt_start) & (shots_df["shotDistance"] < filt_start + filt_width)]
gdf = filt_df.groupby("shot_zone").agg({"shot_made": "sum", "period": "count"})
gdf = gdf.reset_index().rename({"period": "shot_atts"}, axis=1)
gdf = gdf.assign(team="NBA")
gdf = gdf.assign(filt_start=filt_start)
gdf = gdf.assign(shot_freq=gdf.shot_atts / len(shots_df))
gdf = gdf.assign(shot_acc=gdf.shot_made / gdf.shot_atts)
gdf_list.append(gdf)
filt_start += filt_inc
gdf = pd.concat(gdf_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment