Home > Software design >  How to use Python Pandas to calculate the mean for skipped backward rows?
How to use Python Pandas to calculate the mean for skipped backward rows?

Time:10-27

Here is the data:

data = {'col1': [12, 13, 5, 2, 12, 12, 13, 23, 32, 65, 33, 52, 63, 12, 42, 65, 24, 53, 35]}
df = pd.DataFrame(data)

I want to create a new col skipped_mean. Only the last 3 rows have a valid value for this variable. What it does is it looks back 6 rows backward, continuously for 3 times, and take the average of the three numbers

enter image description here

How can it be done?

CodePudding user response:

You could do it with a weighted rolling mean approach:

import numpy as np

weights = np.array([1/3,0,0,0,0,0,1/3,0,0,0,0,0,1/3])
df['skipped_mean'] = df['col1'].rolling(13).apply(lambda x: np.sum(weights*x))
  • Related