pandas_滑动窗口

import pandas as pd
import numpy as np
%matplotlib inline
#pd.date_range()
df = pd.Series(np.random.randn(600),index=pd.date_range('7-1-2016',periods=600,freq='D')) df 2016-07-01 -0.316216 2016-07-02 -0.111523 2016-07-03 1.230472 2016-07-04 1.374675 2016-07-05 -0.850903 ... 2018-02-16 0.583057 2018-02-17 -1.240478 2018-02-18 0.606154 2018-02-19 0.813593 2018-02-20 0.854716 Freq: D, Length: 600, dtype: float64
#进行滑动窗口 r
= df.rolling(window=10) Rolling [window=10,center=False,axis=0] r.mean().head(11) #NaN的原因是因为小于窗口的长度 2016-07-01 NaN 2016-07-02 NaN 2016-07-03 NaN 2016-07-04 NaN 2016-07-05 NaN 2016-07-06 NaN 2016-07-07 NaN 2016-07-08 NaN 2016-07-09 NaN 2016-07-10 0.146395 2016-07-11 0.223922 Freq: D, dtype: float64 import matplotlib.pyplot as plt plt.figure(figsize=(15,5)) df.plot(style='r--') df.rolling(window=10).mean().plot(style='b')

pandas_滑动窗口_第1张图片

 

你可能感兴趣的:(pandas_滑动窗口)