I am trying to calculate moving average of a dataframe column. The column has some nans. I tried an already available solution provided here. However, the result is nans only.
My code:
df =
A
0 10
1 NaN
2 20
3 30
4 40
5 50
6 60
7 NaN
def running_mean(x, N):
cumsum = np.cumsum(np.insert(x, 0, 0))
return (cumsum[N:] - cumsum[:-N]) / float(N)
x = running_mean(df['A'].values, 2)
plt.plot(x)
plt.show()
Present output:
x = array([nan, nan, nan, nan, nan, nan, nan])