Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calculating mean of column based on the occurence of a number in another column Pandas dataframe Python

I’ve got a Pandas dataframe like the one below. What I’m trying to do is calculating the mean of column s2, for every time that ‘5’ occures in s1.

s1  s2
5   0.5
1   0.43
5   1
5   1

In this case, 5 occures three times, so we take the average over 0.5+1+1=0.83.
Can someone help me to calculate this? Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this

df[df['s1']==5]['s2'].mean()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading