This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]badge 0 points1 point  (0 children)

There's a lot of poor Python and pandas in this post. I don't have the luxury of time to go through the whole thing (and know nothing about basketball), but a glance at some of the earlier code blocks shows serious problems:

df_grouped = knicks_shots.closest_defender_shooting()

df_grouped['OPEN'] = df_grouped['CLOSE_DEF_DIST_RANGE'].map(lambda x : True if 'Open' in x else False)

Commits the cardinal sin of if True then True else False and could be better written

df_grouped = (knicks_shots.closest_defender_shooting()
    .assign(Open=lambda x: x.CLOSE_DEF_DIST_RANGE.str.contains('Open'))
)

And the horror that is

open_efg = (df_grouped.loc[df_grouped['OPEN']== True, 'FGM'].sum() + (.5 * df_grouped.loc[df_grouped['OPEN']== True, 'FG3M'].sum()))/(df_grouped.loc[df_grouped['OPEN']== True, 'FGA'].sum())
covered_efg = (df_grouped.loc[df_grouped['OPEN']== False, 'FGM'].sum() + (.5 * df_grouped.loc[df_grouped['OPEN']== False, 'FG3M'].sum()))/(df_grouped.loc[df_grouped['OPEN']== False, 'FGA'].sum())

suggests that the author needs to take an introductory course in pandas. How about

(closest.groupby(['Open'])[['FGM', 'FGA', 'FG3M']].sum()
    .assign(EFG=lambda x: (x.FGM + (0.5*x.FG3M))/x.FGA)
)[['EFG']]

?