pandas rolling() – Mean, Average, Sum Examples

pandas.DataFrame.rolling() function can be used to get the rolling mean, average, sum, median, max, min e.t.c for one or multiple columns. Rolling mean is also known as the moving average, It is used to get the rolling window calculation.

Rolling and moving averages are used to analyze the data for a specific time series and to spot trends in that data.

rolling() Key Points:

  • It supports to calculate rolling mean, average, max, min, sum, count, median, std e.t.c
  • By default it uses window type as None and provides a way to change it.
  • It suports to specify minimum periods.

1. DataFrame.rolling() Syntax

Following is the syntax of DataFrame.rolling() function. Returns a window of rolling subclass.


# Syntax of DataFrame.rolling()
DataFrame.rolling(window, min_periods=None, center=False, win_type=None, on=None, axis=0, closed=None, method='single')

Use the window param to specify the size of the moving window. min_periods is used to specify the minimum number of observations in the window. if the minimum number is not present it results in NA.

Use win_type=None to have all points are evenly weighted.

First, let’s create a pandas DataFrame to explain rolling() with examples


# Create DataFrame
import pandas as pd
import numpy as np

df = pd.DataFrame({'A': [0,1,2,4,6,10,4],
                   'B': [0,1,3,6,9,np.nan,4]})
print(df)

# Outputs
#    A    B
#0   0  0.0
#1   1  1.0
#2   2  3.0
#3   4  6.0
#4   6  9.0
#5  10  NaN
#6   4  4.0

2. pandas rolling() Example

rolling() function returns a subclass of Rolling with the values used to calculate.


# Returns Rolling subclass.
rolling=df.rolling(window=2)
print(rolling)

# Outputs
Rolling [window=2,center=False,axis=0,method=single]

Now, let’s do the rolling sum with window=2. 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.


# Rolling() of sum with window length 3
df2=df.rolling(window=3).sum()
print(df2)

# Outputs
#      A     B
#0   NaN   NaN
#1   NaN   NaN
#2   3.0   4.0
#3   7.0  10.0
#4  12.0  18.0
#5  20.0   NaN
#6  20.0   NaN

3. pandas rolling() mean

You can also calculate the mean or average with pandas.DataFrame.rolling() function, rolling mean is also known as the moving average, It is used to get the rolling window calculation. This use win_type=None, meaning all points are evenly weighted.


# Rolling() of mean with window length 3
df2=df.rolling(window=3).mean()
print(df2)

# Outputs
#          A         B
#0       NaN       NaN
#1       NaN       NaN
#2  1.000000  1.333333
#3  2.333333  3.333333
#4  4.000000  6.000000
#5  6.666667       NaN
#6  6.666667       NaN

4. By using Triange mean

Following example does the rolling mean with a window length of 3, using the ‘triang’ window type.


# Rolling() of sum with win_type triang
df2=df.rolling(window=3, win_type='triang').mean()
print(df2)


# Outputs
#      A     B
#0   NaN   NaN
#1   NaN   NaN
#2  1.00  1.25
#3  2.25  3.25
#4  4.00  6.00
#5  6.50   NaN
#6  7.50   NaN

5. Using gaussian

Following example does the rolling mean with a window length of 3, using the ‘gaussian’ window type. With Gaussian window type, you have to provide the std param. Not using this results in TypeError: gaussian() missing 1 required positional argument: ‘std’


# Rolling() of sum with window type gaussian
df2=df.rolling(window=3, win_type='gaussian').mean(std=3)
print(df2)

# Outputs
#          A         B
#0       NaN       NaN
#1       NaN       NaN
#2  1.000000  1.327104
#3  2.327104  3.327104
#4  4.000000  6.000000
#5  6.654209       NaN
#6  6.728956       NaN

6. Rollings Sum & Min

The below example provides multiple rollings using agg() function, It does the window length of 2, and performs sum on column A and min on column B.


# Rolling agg on multuple columns
df2 = df.rolling(2).agg({"A": "sum", "B": "min"})
print(df2)

# Outputs
#      A    B
#0   NaN  NaN
#1   1.0  0.0
#2   3.0  1.0
#3   6.0  3.0
#4  10.0  6.0
#5  16.0  NaN
#6  14.0  NaN

Conclusion

In this article, you have learned the syntax of the rolling() function and how to calculate the rolling mean, average, median and sum by using different parameters with examples. It supports rolling to calculate mean, max, min, sum, count, median, std e.t.c

References

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing pandas rolling() – Mean, Average, Sum Examples