Pandas how to calculate bygroup result based on the length of the each group and a count value of another column

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Pandas how to calculate bygroup result based on the length of the each group and a count value of another column



I want to calculate the scoring rate of each zone by using bygroup in pandas, but not sure how to do it:



Suppose the df has two columns as:


Shot_type Shot_zone
Goal Penalty_area
Saved Penalty_area
Goal Goal Box
Saved Goal Box



Here I want to groupy by Shot_zone, and calculate the scoring rate based on Shot_type's Goal counts / len() of each type Shot_zone. Here each Shot_zone has 1 goal and 1 saved, so the result should be like:


Penalty_area 50%
Goal Box 50%



Is there any understandable approach to do so using Pandas?
Thank you very much!




3 Answers
3



Using


pd.crosstab(df.Shot_type,df.Shot_zone,normalize='index')
Out[662]:
Shot_zone GoalBox Penalty_area
Shot_type
Goal 0.5 0.5
Saved 0.5 0.5





Thanks Wen, this gets me more than what I want!
– commentallez-vous
2 mins ago





@commentallez-vous you can always using .loc for select the index you need
– Wen
1 min ago





Got it. I know both iloc and loc, but not sure how to work on the bygroup object...I am just learning by using it, but totally new to pandas
– commentallez-vous
43 secs ago



One way is to binarize your Shot_type column, i.e. set to True if it equals 'Goal', and then use GroupBy + mean:


Shot_type


True


'Goal'


GroupBy


mean


res = df.assign(Shot_type=df['Shot_type']=='Goal')
.groupby('Shot_zone')['Shot_type'].mean()

print(res)

Shot_zone
GoalBox 0.5
Penalty_area 0.5
Name: Shot_type, dtype: float64





Thank you jpp, great answer, though I actually had 4 different Shot_type haha
– commentallez-vous
1 min ago





@commentallez-vous, Ah, then use crosstab :)
– jpp
1 min ago


crosstab





Thanks man! cheers
– commentallez-vous
27 secs ago



Can also groupby and apply


groupby


apply


df.groupby('Shot_zone').Shot_type.apply(lambda s: '{}%'.format((s[s=='Goal']).size/(s.size) * 100))

Shot_zone
Goal_Box 50.0%
Penalty_area 50.0%





wow, this is quite elegant!
– commentallez-vous
2 mins ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results