I could not find a direct solution to the general problem of using multiple columns in rolling- but in your specific case, you can just take the average of columns A and B, and then apply yours rolling:
df['A_B_moving_average'] = ((df.A + df.B) / 2).rolling(window=50, axis='rows').mean()
, : DataFrame rolling axis='rows', . :
df['A_B_moving_average'] = df.rolling(window=5, axis='rows').mean()
A (), B (), DateTime ( , , ). NumPy, " ". print s:
import numpy.random as rnd
import pandas as pd
import numpy as np
count = 10
dates = pd.date_range('1/1/2010', periods=count, freq='D')
df = pd.DataFrame(
{
'DateTime': dates,
'A': rnd.normal(50, 2, count),
'B': rnd.normal(100, 4, count)
}, index=dates
)
df[['A', 'B']].rolling(window=6, axis='rows').apply(lambda row: print(row) or np.max(row))
:
[ 47.32327354 48.12322447 50.86806381 49.3676319 47.81335338
49.66915104]
[ 48.12322447 50.86806381 49.3676319 47.81335338 49.66915104
48.01520798]
[ 50.86806381 49.3676319 47.81335338 49.66915104 48.01520798
48.14089864]
[ 49.3676319 47.81335338 49.66915104 48.01520798 48.14089864
51.89999973]
[ 47.81335338 49.66915104 48.01520798 48.14089864 51.89999973
48.76838054]
[ 100.10662696 96.72411985 103.24600664 95.03841539 95.23430836
102.30955102]
[ 96.72411985 103.24600664 95.03841539 95.23430836 102.30955102
95.18273088]
[ 103.24600664 95.03841539 95.23430836 102.30955102 95.18273088
97.36751546]
[ 95.03841539 95.23430836 102.30955102 95.18273088 97.36751546
99.25325622]
[ 95.23430836 102.30955102 95.18273088 97.36751546 99.25325622
105.16747544]
A B, .