Python 大熊猫的移动平均线,发布第一行和最后一行

Python 大熊猫的移动平均线,发布第一行和最后一行,python,pandas,dataframe,mean,Python,Pandas,Dataframe,Mean,我在计算数据帧列上5个时段的移动平均值。这是我写的代码: df['Volume_moving'] = df['Volume'].rolling(window = 5, center = True).mean() 这是创建了新列的数据集: Open High Low Close Volume Adj Close Volume_moving Date 2012-10-15 632.35

我在计算数据帧列上5个时段的移动平均值。这是我写的代码:

df['Volume_moving'] = df['Volume'].rolling(window = 5, center = True).mean()
这是创建了新列的数据集:

             Open    High    Low    Close   Volume    Adj Close Volume_moving
Date                            
2012-10-15  632.35  635.13  623.85  634.76  15446500    631.87  NaN
2012-10-16  635.37  650.30  631.00  649.79  19634700    646.84  NaN
2012-10-17  648.87  652.79  644.00  644.61  13894200    641.68  18514440.0
2012-10-18  639.59  642.06  630.00  632.64  17022300    629.76  19330360.0
2012-10-19  631.05  631.77  609.62  609.84  26574500    607.07  20454460.0
... ... ... ... ... ... ... ... .... .... ....     ...... ..... ........ 
2013-01-08  529.21  531.89  521.25  525.31  16382400    525.31  18185340.0
2013-01-09  522.50  525.01  515.99  517.10  14557300    517.10  16443720.0
2013-01-10  528.55  528.72  515.52  523.51  21469500    523.51  18221260.0
2013-01-11  521.00  525.32  519.02  520.30  12518100    520.30  NaN
2013-01-14  502.68  507.50  498.51  501.75  26179000    501.75  NaN
“Volume_moving”列的值正确,但第一行和最后两行的值为NaN,现在对于x=1和x=2,我希望分别使用三行和四行计算移动平均值(最后两行相同)。我该怎么做


提前谢谢。

请分享示例数据。对不起,刚刚添加!只需在滚动功能中使用
min\u periods=3
选项即可。这会解决你的问题!您可以尝试不使用中心超参数,这将意味着前4行将为空。这不是我需要的,我需要中心参数。请共享示例数据。对不起,刚刚添加!只需在滚动功能中使用
min\u periods=3
选项即可。这会解决你的问题!您可以尝试不使用中心超参数,这将意味着前4行将为空。这不是我需要的,我需要中心参数。