Home > Back-end >  Pandas: how to calculate average ignoring 0 within groups?
Pandas: how to calculate average ignoring 0 within groups?

Time:12-16

My data looks like this:

It is grouped by "name"

                           name  star  atm  food  foodcp  drink  drinkcp  clean  cozy  service  
     ___Backyard Jr. (__Xinyi)     4    4     4       4      4        0      4     0        0      
     ___Backyard Jr. (__Xinyi)     3    0     3       0      3        0      0     0        3      
     ___Backyard Jr. (__Xinyi)     4    0     0       0      4        0      0     0        0      
     ___Backyard Jr. (__Xinyi)     3    0     0       0      0        0      0     3        3      

I want to calculate the mean of all columns except for name, which will ignore the "0" and it will be done within groups. How can I do it?

I've tried use

df.groupby('name',as_index=False).mean()

but it dose calculate the "0".

Thank you for your help!!

CodePudding user response:

You can first replace all the zeros by NaN:

df = df.replace(0, np.nan)

These nan values will be excluded from your mean.

  • Related