简体   繁体   中英

window based weighted average in pandas

I am trying to do a window based weighted average of two columns

for example if i have my value column "a" and my weighting column "b"

   a b
1: 1 2
2: 2 3
3: 3 4

with a trailing window of 2 (although id like to work with a variable window length)

my third weighted average column should be "c" where the rows that do not have enough previous data for a full weighted average are nan

   c
1: nan
2: (1 * 2 + 2 * 3) / (2 + 3) = 1.8
3: (2 * 3 + 3 * 4) / (3 + 4) = 2.57

For your particular case of window of 2, you may use prod and shift

s = df.prod(1)
(s + s.shift()) / (df.b + df.b.shift())

Out[189]:
1         NaN
2    1.600000
3    2.571429
dtype: float64

On sample df2 :

       a      b
0  73.78  51.46
1  73.79  27.84
2  73.79  34.35

s = df2.prod(1)
(s + s.shift()) / (df2.b + df2.b.shift())

Out[193]:
0          NaN
1    73.783511
2    73.790000
dtype: float64

This method still works on variable window length. For variable window length, you need additional listcomp and sum

Try on sample df2 above

s = df2.prod(1)

m = 2  #window length 2
sum([s.shift(x) for x in range(m)]) / sum([df2.b.shift(x) for x in range(m)])

Out[214]:
0          NaN
1    73.783511
2    73.790000
dtype: float64

On window length 3

m = 3  #window length 3
sum([s.shift(x) for x in range(m)]) / sum([df2.b.shift(x) for x in range(m)])

Out[215]:
0          NaN
1          NaN
2    73.785472
dtype: float64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM