How to Calculate a Rolling Maximum in Pandas (With Examples)


You can use the following methods to calculate a rolling maximum value in a pandas DataFrame:

Method 1: Calculate Rolling Maximum

df['rolling_max'] = df.values_column.cummax()

Method 2: Calculate Rolling Maximum by Group

df['rolling_max'] = df.groupby('group_column').values_column.cummax()

The following examples show how to use each method in practice.

Example 1: Calculate Rolling Maximum

Suppose we have the following pandas DataFrame that shows the sales made each day at some store:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'day': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
                   'sales': [4, 6, 5, 8, 14, 13, 13, 12, 9, 8, 19, 14]})

#view DataFrame
print(df)

    day  sales
0     1      4
1     2      6
2     3      5
3     4      8
4     5     14
5     6     13
6     7     13
7     8     12
8     9      9
9    10      8
10   11     19
11   12     14

We can use the following syntax to create a new column that displays the rolling maximum value of sales:

#add column that displays rolling maximum of sales
df['rolling_max'] = df.sales.cummax()

#view updated DataFrame
print(df)

    day  sales  rolling_max
0     1      4            4
1     2      6            6
2     3      5            6
3     4      8            8
4     5     14           14
5     6     13           14
6     7     13           14
7     8     12           14
8     9      9           14
9    10      8           14
10   11     19           19
11   12     14           19

The new column titled rolling_max displays the rolling maximum value of sales.

Example 2: Calculate Rolling Maximum by Group

Suppose we have the following pandas DataFrame that shows the sales made each day at two different stores:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'store': ['A', 'A', 'A', 'A', 'A', 'A',
                             'B', 'B', 'B', 'B', 'B', 'B'],
                   'day': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
                   'sales': [4, 6, 5, 8, 14, 13, 13, 12, 9, 8, 19, 14]})

#view DataFrame
print(df)

   store  day  sales
0      A    1      4
1      A    2      6
2      A    3      5
3      A    4      8
4      A    5     14
5      A    6     13
6      B    7     13
7      B    8     12
8      B    9      9
9      B   10      8
10     B   11     19
11     B   12     14

We can use the following syntax to create a new column that displays the rolling maximum value of sales grouped by store:

#add column that displays rolling maximum of sales grouped by store
df['rolling_max'] = df.groupby('store').sales.cummax()

#view updated DataFrame
print(df)

   store  day  sales  rolling_max
0      A    1      4            4
1      A    2      6            6
2      A    3      5            6
3      A    4      8            8
4      A    5     14           14
5      A    6     13           14
6      B    7     13           13
7      B    8     12           13
8      B    9      9           13
9      B   10      8           13
10     B   11     19           19
11     B   12     14           19

The new column titled rolling_max displays the rolling maximum value of sales, grouped by store.

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

How to Drop Rows in Pandas DataFrame Based on Condition
How to Filter a Pandas DataFrame on Multiple Conditions
How to Use “NOT IN” Filter in Pandas DataFrame

Leave a Reply

Your email address will not be published.