Pandas DataFrame.rolling(n).sum()
function is used to get the sum of rolling windows over a DataFrame. Using this function we can get the rolling sum for single or multiple columns of a given Pandas DataFrame. Where n
is the size of window. series.rolling(n).sum()
function also is used to calculate the rolling sum for Pandas Series. In this article, I will explain how to calculate the rolling sum of pandas DataFrame and series with examples.
1. Quick Examples of Pandas Rolling Sum
If you are in a hurry, below are some quick examples of pandas rolling sum of DataFrame and Series.
# Example 1: Rolling sum of pandas series
ser2 = ser.rolling(3).sum()
# Example 2: Use series.rolling().sum() function
ser2 = ser.rolling(3, center=True).sum()
# Example 3: Rolling sum of single columns
df2 = df['A'].rolling(3).sum()
# Example 4: Use DataFrame.rolling().sum()
# Rolling Sum of multiple columns
df2 = df.rolling(2).sum()
# Example 5: Rolling Sum of multiple columns
df2=df.rolling(window=2).sum()
2. Rolling Sum of Pandas Series
Pandas Series is a one-dimensional, Index-labeled data structure available in the Pandas library. It can store all the datatypes such as strings, integers, float, and other python objects. We can access each element in the Series with the help of corresponding default indices.
Now, let’s create pandas series using a list of values.
import pandas as pd
import numpy as np
# Initialize pandas series
ser = pd.Series([0, 3, 6, 9, 8, 12])
print(ser)
Yields below output.
# Output:
0 0
1 3
2 6
3 9
4 8
5 12
dtype: int64
2.1 Using Series.rolling().sum() Example
You can use Series.rolling()
function to get a rolling window over a pandas series and then apply the sum()
function to get the rolling sum over the window. Here, 3
is the size of the window you want to use it will return a rolling sum for all the numerical columns in the Series.
# Rolling sum of pandas series
ser2 = ser.rolling(3).sum()
print(ser2)
Yields below output.
# Output:
0 NaN
1 NaN
2 9.0
3 18.0
4 23.0
5 29.0
dtype: float64
Now, let’s do the rolling sum series with window=3
. By default, the result is set to the right edge of the window. You can change this to the center of the window by setting center=True
.
# Use series.rolling().sum() function
ser2 = ser.rolling(3, center=True).sum()
print(ser2)
Yields below output.
# Output:
0 NaN
1 9.0
2 18.0
3 23.0
4 29.0
5 NaN
dtype: float64
3. Rolling Sum of Pandas DataFrame
Now, Let’s create Pandas DataFrame using data from a Python dictionary, where the columns are A
and B
.
import pandas as pd
import numpy as np
# Initialize pandas dataframe
df = pd.DataFrame({'A': [0,2,4,5,8,10,12],
'B': [0,1,3,7,9,np.nan,15]})
print(df)
Yields below output.
# Output:
A B
0 0 0.0
1 2 1.0
2 4 3.0
3 5 7.0
4 8 9.0
5 10 NaN
6 12 15.0
3.1 Use DataFrame.rolling().sum()Example
You can get the three rolling sums of the "A"
column, you apply the rolling()
function with a window size of three and then apply the sum()
function. You get NaN values in the first two rows because you cannot calculate the rolling sum as there are no preceding values to make the three windows complete. The third row is 6.0
which is the sum of "A"
over the three windows containing 0
, 2
, and 4
.
# Rolling sum of single columns
df2 = df['A'].rolling(3).sum()
print(df2)
Yields below output.
# Output:
0 NaN
1 NaN
2 6.0
3 11.0
4 17.0
5 23.0
6 30.0
Name: A, dtype: float64
3.2 Rolling Sum of Multiple Columns
Similarly, You can use DataFrame.rolling()
function to get the two windows rolling sum of multiple columns and then apply the sum()
function to get the rolling sum over the window. It will calculate the rolling sum for all the numerical columns in the DataFrame. For example, let’s get the two rolling sums of multiple columns in DataFrame.
# Use DataFrame.rolling().sum()
# Rolling Sum of multiple columns
df2 = df.rolling(2).sum()
print(df2)
# Rolling Sum of multiple columns
df2=df.rolling(window=2).sum()
print(df2)
Yields below output.
# Output:
A B
0 NaN NaN
1 2.0 1.0
2 6.0 4.0
3 9.0 10.0
4 13.0 16.0
5 18.0 NaN
6 22.0 NaN
4. Complete Example For Rolling Sum
import pandas as pd
import numpy as np
# Initialize pandas dataframe
df = pd.DataFrame({'A': [0,2,4,5,8,10,12],
'B': [0,1,3,7,9,np.nan,15]})
print(df)
# Rolling sum of single columns
df2 = df['A'].rolling(3).sum()
print(df2)
# Use DataFrame.rolling().sum()
# Rolling Sum of multiple columns
df2 = df.rolling(2).sum()
print(df2)
5. Conclusion
In this article, I have explained how to get pandas rolling sum of Series and DataFrame by using series.rolling().sum()
, and DataFrame.rolling().sum()
functions respectively.
Happy Learning !!
Related Articles
- pandas rolling() Mean, Average, Sum Examples
- Pandas Sum DataFrame Columns With Examples
- Pandas Get Total | Sum of Column
- Pandas Count Rows with Condition
- How to Get Pandas Columns Count
- Pandas melt() DataFrame Example
- Pandas Filter by Index
- How to Get Pandas Columns Count
- How to Count Duplicates in Pandas DataFrame
- How to Get Column Average or Mean in pandas DataFrame