I have numpy array-2D (pandas DatarFame can be also used) full of numbers and I need to create / replace those numbers with mean for last n rows in one column. I have huge numpy array.shape like [10000:10000]
Example (limited shape just for explanation):
Numpy Array:
[[10, 30, 8, 1],
[11, 5, 19, 12],
[12, 18, 15, 6],
[13, 10, 21, 9],
[14, 67, 14, 2],
[15, 13, 12, 6]]
Average by n = 3
So the code should take last 3 numbers in iteration and crate average
Numpy Array:
[[12.5, 23.5, 14.83333333, 5.833333333],
[12, 10.33333333, 18.33333333, 9],
[13, 31.66666667, 16.66666667, 5.666666667],
[14, 30, 15.66666667, 5.333333333]]
Explanation:
- 14 is average of numbers 15,14,13
- 18.33333333 is average of numbers 21, 15, 19
- 9 is average of numbers 9, 6, 12
Result should be that function takes n-last values in column dimension and make average of it.
I was able to do it through 2 for loops and standard python code, but it takes a lot of time.