Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Dataframe, average non 0 value

I have the following Pandas Dataframe 'df':

a1  a2  a3  b1
0   0   0   1
1   2   0   2
3   0   0   3
2   4   0   4

How do I get the average value of "a" for from a1, a2, a3, ignoring the 0 value?

a1  a2  a3  b1  avg(a)
0   0   0   1   0
1   2   0   2   1.5
3   0   0   3   3.0
2   4   0   4   3.0

I'm stuck with manual approach where I convert the value > 0 to 1

like image 732
newbs_dasc Avatar asked Apr 24 '26 14:04

newbs_dasc


1 Answers

You can .filter the a like columns, then .mask the zeros in these columns and take .mean along axis=1:

a = df.filter(like='a')
df['avg'] = a.mask(a.eq(0)).mean(1).fillna(0)

# OR df['avg'] = a[a > 0].mean(1).fillna(0)

   a1  a2  a3  b1  avg
0   0   0   0   1  0.0
1   1   2   0   2  1.5
2   3   0   0   3  3.0
3   2   4   0   4  3.0
like image 118
Shubham Sharma Avatar answered Apr 26 '26 05:04

Shubham Sharma