dataframe rolling window 1

dataframe rolling window

>>> df.rolling(2, min_periods=1).sum()
     B
0  0.0
1  1.0
2  3.0
3  2.0
4  4.0

Here is what the above code is Doing:
1. We create a DataFrame with a single column of random numbers.
2. We then create a rolling object that has a window size of 2.
3. We then apply the rolling object’s sum() method to the DataFrame.
4. We then print the results.

The rolling object’s sum() method returns a new DataFrame that has the same index as the original DataFrame.
The new DataFrame has a single column, which is the sum of the values in the original DataFrame’s column.

The rolling object’s sum() method has a min_periods parameter.
This parameter specifies the minimum number of observations in the window required to have a value (otherwise the value is NaN).

In the above code, we set the min_periods parameter to 1.
This means that the rolling object’s sum() method will return a value for every row in the DataFrame.

If we set the min_periods parameter to 2, then the rolling object’s sum() method will only return a value for the second row and all subsequent rows.

Similar Posts