0

Assume I have a M (rows) by N (columns) dataFrame

df = pandas.DataFrame([...])

and a vector of length N

windows = [1,2,..., N]

I would like to apply a moving average function to each column in df, but would like the moving average to have different length for each column (e.g. column1 has MA length 1, column 2 has MA length 2, etc) - these lengths are contained in windows

Are there built in functions to do this quickly? I'm aware of the df.apply(lambda a: f(a), axis=0, args=...) but unclear how to apply different args for each column

cs95
  • 379,657
  • 97
  • 704
  • 746
Michael
  • 7,087
  • 21
  • 52
  • 81

1 Answers1

1

Here's one way to do it:

In [15]: dfrm
Out[15]: 
          A         B         C
0  0.948898  0.587032  0.131551
1  0.385582  0.275673  0.107135
2  0.849599  0.696882  0.313717
3  0.993080  0.510060  0.287691
4  0.994823  0.441560  0.632076
5  0.711145  0.760301  0.813272
6  0.932131  0.531901  0.393798
7  0.965915  0.812821  0.287819
8  0.782890  0.478565  0.960353
9  0.908078  0.850664  0.912878

In [16]: windows
Out[16]: [1, 2, 3]

In [17]: pandas.DataFrame(
    {c: dfrm[c].rolling(windows[i]).mean() for i, c in enumerate(dfrm.columns)}
)
Out[17]: 
          A         B         C
0  0.948898       NaN       NaN
1  0.385582  0.431352       NaN
2  0.849599  0.486277  0.184134
3  0.993080  0.603471  0.236181
4  0.994823  0.475810  0.411161
5  0.711145  0.600931  0.577680
6  0.932131  0.646101  0.613049
7  0.965915  0.672361  0.498296
8  0.782890  0.645693  0.547323
9  0.908078  0.664614  0.720350

As @Manish Saraswat mentioned in the comments, you can also express the same thing as dfrm[c].rolling_mean(windows[i]). Further, you can use sequences as the items in windows if you want, and they would express a custom window shape (size and weights), or any of the other options with different rolling aggregations and keywords.

ely
  • 74,674
  • 34
  • 147
  • 228