[Pandas] [2] Understanding of moving window rolling

concept:

​​In order to improve the accuracy of the data, the value of a certain point is expanded to an interval that contains this point, and the interval is used for judgment. This interval is the window. Moving the window means that the window slides to one end. The default is from right to left. Each slide is not a whole block of the interval, but a unit of a unit. Give an example to better understand:

import pandas as pd
s = [1,2,3,5,6,10,12,14,12,30]
pd.Series(s).rolling(window=3).mean()

First of all, for better understanding, first put pd.Series(s) on it for everyone to see 
Write picture description here

Now after pd.Series(s).rolling(window=3).mean() is processed 
Write picture description here

I don’t know if you can see the rule. 
First, we set the window window=3, that is, take an average of 3 numbers. Index 0,1 is NaN, because there are not enough 3 numbers in front of them. When it comes to index2, how is its value calculated? That is (index0+index1+index2)/3 
index3 is (index1+index2+ index3) / 3

Detailed parameter

DataFrame.rolling(window, min_periods=None, center=False, win_type=None, on=None, axis=0, closed=None)
  •  

window:  You can also omit it. Indicates the size of the time window. Note that there are two forms (int or offset). If int is used, the value indicates the number of observations for calculating the statistics, that is, the previous few data. If it is the offset type, it indicates the size of the time window. Offset detailed explanation  
min_periods : The minimum number of observations contained in each window. The result of the window less than this value is NA. The value can be int, and the default is None. In the case of offset, the default is 1. 
center:  Set the label of the window to center. Boolean, default False, right 
win_type:  window type. Various functions of the interception window. String type, default is None. Various types  
on:  optional parameters. For the dataframe, specify the column to calculate the rolling window. The value is the column name. 
axis:  int, string, the default is 0, that is to calculate the column 
closed: define the opening and closing of the interval, support int type window. For the offset type, the default is left open and right closed, that is, the default is right. It can be specified as left both etc. according to the situation.

import pandas as pd
s = [1,2,3,5,None,10,12,14,12,30]
pd.Series(s).rolling(3,min_periods=2).mean()

Write picture description here

 

Reprinted from: https://blog.csdn.net/maymay_/article/details/80241627

Guess you like

Origin blog.csdn.net/xiezhen_zheng/article/details/82319183