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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With