window: Size of the window min_periods : Minimum number of observation required to have a value. center Bool, default : False, set the window Label to right edge. win_type : None, If a string, it must be a valid scipy.signal window function on : a column label or Index level on which to calculate the rolling window. axis : 0 ( defalut ) or 1 , Axis = 0 is across rows, axis = 1 is across columns. closed : None (default), 'right', 'left','both','neither'. Points to exclude from calculation. step : None ( default ), int , Evaluate the window at every step result method : single ( default ) or table, how to execute rolling operation
import pandas as pd
my_dict={
'name':['Alex','King','Ravi','Raju','John'],
'mark':[7,8,5,6,3]
}
df = pd.DataFrame(data=my_dict) # create DataFrame
#print(df.mark.rolling(2).sum()) # rolling window values
df['my_sum']=df.mark.rolling(2).sum() # New column with rolling window values
print(df)
Output
name mark my_sum
0 Alex 7 NaN
1 King 8 15.0
2 Ravi 5 13.0
3 Raju 6 11.0
4 John 3 9.0
min_periods
In above output you can see the first row value for rolling window ( column my_sum ) is NaN. This is because we need an window of two values ( current and previous one ) to get sum value. We can set the min_periods to 1 to specify window length of 1 if two rows are not available. This will change the value of first row only.
mark math
0 NaN 77.0
1 NaN 88.0
2 NaN 55.0
3 NaN 66.0
4 NaN 33.0
If you have more than one non-numeric column then we have to specify the column for the rolling window to apply. We may get this error if we don't specify the column name.
import pandas as pd
my_dict={
'name':['Alex','King','Ravi','Raju','John'],
'class1':['One','Two','Three','Four','Five'],
'mark':[7,8,5,6,3],
'math':[70,80,50,60,3]
}
df = pd.DataFrame(data=my_dict) # create DataFrame
df['my_sum']=df.rolling(2).math.sum() # rolling window on math column
print(df)
Output
name class1 mark math my_sum
0 Alex One 7 70 NaN
1 King Two 8 80 150.0
2 Ravi Three 5 50 130.0
3 Raju Four 6 60 110.0
4 John Five 3 3 63.0
Rolling window using Date
Here more than one record is available in a single day. Here 2 days as considered as window, so multiple rows are used for calculated the values within 2 days rolling.
Here we are using to_datetime() to convert the column to datetime dtype.
Here we have used two different date columns dt_sale and dt_buy. Two different columns are created by using the windows of these two columns for 2 days.