Answer the question
In order to leave comments, you need to log in
How to get the average of the last n rows into a new column in pandas?
Greetings. I'll write it in more detail, I don't know how to google it.
There is a table:
a =
df = pd.DataFrame(a, columns=['low', 'high'])
df
df = df.assign(hl2 = (df.low + df.high) / 2)
df
df = df.assign(mean3 = df.tail(3).hl2.mean())
df
Answer the question
In order to leave comments, you need to log in
Found the answer. rolling method
df = df.assign(mean3 = df.rolling(3, min_periods=1).hl2.mean())
df
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question