Kulwant
Kulwant

Reputation: 681

Pandas - Calculating Average of Field and Multiply By Number of Occurrences

Here is a data frame. I want to calculate the average ratio of play type(short_pass, long_pass, deep_pass) and multiply it by how often the play_type occurred.

I can do a group_by play_type and get individual mean, but am stuck on getting the number of times a play_type occurred( short pass occurs twice) and then multiplying the two.

Thanks!

Quarterback   Play_Type Ratio

Brady   Short_Pass    5.4
Brady   Long_Pass     7.2
Brady   Deep_Pass     8.1
Rodgers Long_Pass     6.4
Rodgers Deep_Pass     7.2
Miller  Short_Pass    4.2
Miller  Deep_Pass     7.3

Upvotes: 2

Views: 307

Answers (1)

piRSquared
piRSquared

Reputation: 294488

g = df.groupby('Play_Type')
g.Ratio.mean() * g.Play_Type.count()

Play_Type
Deep_Pass     22.6
Long_Pass     13.6
Short_Pass     9.6
dtype: float64

However, this is the same as the sum

g = df.groupby('Play_Type')
g.Ratio.sum()

Play_Type
Deep_Pass     22.6
Long_Pass     13.6
Short_Pass     9.6
Name: Ratio, dtype: float64

Upvotes: 1

Related Questions