I have a dataframe called pitches with every pitch from the 2019 MLB season with columns for pitcher_id and pitch type.
I want to group by pitcher_id and count instances of each pitch type, and I can do so with a groupby().agg() method such as the following:
pitches.groupby('pitcher_id')['pitchType'].agg(
[('Four-Seam Fastball',lambda pitchType: (pitchType=='Four-Seam Fastball').sum()),
('Curveball',lambda pitchType: (pitchType=='Curveball').sum())]
)
I'd like to be able to do this with a list comprehension, but when I try it the resulting columns return all zeros. Here's what I'm trying:
pitch_types = ['Four-Seam Fastball', 'Slider', 'Curveball', 'Changeup', 'Sinker']
pitches.groupby('pitcher_id')['pitchType'].agg(
[(x,lambda pitchType: (pitchType==x).sum()) for x in pitch_types]
)
The first example works flawlessly. The second returns a dataframe with all the pitch types as columns but all zeros for values. Can anyone tell what I'm doing wrong or suggest an alternate approach?