0

I'm trying to figure out how to forward propagate values in Python Pandas in the following way: Basically, let's say I have a Pandas Series (each element is a time t):

[5, 2, 3, 3, 4, 9, 2, 3, 1, 9, 2, 7, 5, 7, 9, 2, 3, 1]

I'd like each element to be lasting 4 time periods, meaning:

[5, 5, 5, 5, ...]

However, as you can see, my 5s are going to overwrite the 2, 3, 3 etc... In my case, I'd like to keep them all but take the average of all.

In this way, this would be:

[5, 3.5, 3.33, 3.25, ...]

I've tried:

myList = [5, 2, 3, 3, 4, 9, 2, 3, 1, 9, 2, 7, 5, 7, 9, 2, 3, 1]
N=4
np.convolve(myList, np.ones((N,))/N, mode='valid')

But I get:

[3.25,
 3.0,
 4.75,
 4.5,
 4.5,
 3.75,
 3.75,
 3.75,
 4.75,
 5.75,
 5.25,
 7.0,
 5.75,
 5.25,
 3.75]

Which is not exactly what I was expecting....

Would you know how to do this? Many thanks

  • 2
    You're looking for a _running average_. Check out the results in this thread: https://stackoverflow.com/questions/13728392/moving-average-or-running-mean – Mark Snyder Jan 22 '20 at 21:41
  • I've tried myList = [5, 2, 3, 3, 4, 9, 2, 3, 1, 9, 2, 7, 5, 7, 9, 2, 3, 1] N=4 np.convolve(myList, np.ones((N,))/N, mode='valid') but this is not getting the same results as i want – JohnHoopHoop Jan 22 '20 at 22:19

1 Answers1

0

Is this what you are after?

s = pd.Series([5, 2, 3, 3, 4, 9, 2, 3, 1, 9, 2, 7, 5, 7, 9, 2, 3, 1])
s.rolling(4, min_periods=1).mean()

0     5.000000
1     3.500000
2     3.333333
3     3.250000
4     3.000000
5     4.750000
6     4.500000
7     4.500000
8     3.750000
9     3.750000
10    3.750000
11    4.750000
12    5.750000
13    5.250000
14    7.000000
15    5.750000
16    5.250000
17    3.750000
dtype: float64
Allen Qin
  • 19,507
  • 8
  • 51
  • 67