Вы можете превратить данные во временные ряды, а затем использовать панд:
groupby().rolling().mean()
Вот код. Сначала сделайте некоторые данные для тестирования:
import pandas as pd
import numpy as np
import string
from datetime import datetime
# set up matches, players, tournament start and end
matches = 50000
players = list(string.ascii_uppercase)
start = datetime(2015, 1, 1).timestamp()
end = datetime(2018, 1, 1).timestamp()
# create a dataframe for testing
df = pd.DataFrame({
'DATE': pd.to_datetime(np.random.randint(start, end, size=matches), unit='s'),
'PLAYER': np.random.choice(players, matches),
'OPPONENT': np.random.choice(players, matches),
'SCORE': np.random.normal(100, 25, matches)
})
# drop the cases where the player played themselve
df = df[df['PLAYER'] != df['OPPONENT']]
# make it a time series and ensure it is sorted
df.set_index('DATE', inplace=True)
df.sort_index(inplace=True)
df.head()

Используйте для него groupby (). Rolling (). Mean ():
df_rolling = df.groupby(['PLAYER', 'OPPONENT']).rolling(3).mean().reset_index()
df_rolling.head()
Присоедините его к исходным данным, содержащим все столбцы, и отметьте один совпадение (A против B)
df_final = pd.merge(df, df_rolling, on=['PLAYER', 'OPPONENT', 'DATE'], suffixes=['_RAW', '_AVG3'])
df_final[df_final['PLAYER'].eq('A') & df_final['OPPONENT'].eq('B')].tail(10)
