I would like to create a column of the average of the 5 highest values from a rolling window of 30. Using a for loop is very slow with a large DataFrame. I tried using rolling() with nlargest(), but it wouldn't work. Any suggestions for speeding this up?
def top_values(df, column, days):
top5 = df.nlargest(days, column)
top = top5[column].sum() / days
x = 0
w = 0
for i in df.index:
if x > 30:
df['tops'][x] = top_values(df[w:x], 'column', 5)
w += 1
x += 1