Window functions in Python are quite useful in estimating descriptive statistics like mean, median, min, max, etc. over a rolling window. In this tutorial, we will look at how to compute the rolling minimum in a pandas column.
How to get rolling minimum in pandas?
You can use the pandas rolling()
function to get a rolling window of your desired size over the series and then apply the pandas min()
function to get the rolling minimum. The following is the syntax –
# s is pandas series, n is the window size s.rolling(n).min()
Here, n
is the size of the moving window you want to use, that is, the number of observations you want to use to compute the rolling statistic, in our case, the minimum. If you apply the above function on a pandas dataframe, it will result in a rolling min for all the numerical columns in the dataframe.
Examples
Let’s look at some examples to see the above syntax in action. First, let’s create a sample dataframe that we will be using throughout this tutorial.
import pandas as pd # create dataframe df = pd.DataFrame({'PageViews': [100, 120, 180, 200, 240, 160, 130], 'Revenue': [10, 15, 12, 20, 30, 22, 14]}, index = ['2020-03-01', '2020-03-02', '2020-03-03', '2020-03-04',\ '2020-03-05', '2020-03-06', '2020-03-07']) # print the dataframe print(df)
Output:
PageViews Revenue 2020-03-01 100 10 2020-03-02 120 15 2020-03-03 180 12 2020-03-04 200 20 2020-03-05 240 30 2020-03-06 160 22 2020-03-07 130 14
We now have a dataframe containing the daily “PageViews” and “Revenue” of a blogging website.
Rolling Min of a Pandas Series
Let’s get the minimum “PageViews” over a 3-day rolling window from the above data. For this, we apply the rolling()
function with a window size of 3
and then apply the min()
function to get the minimum value over that window.
# 3-day rolling minimum of PageViews df['PageViews'].rolling(3).min()
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
2020-03-01 NaN 2020-03-02 NaN 2020-03-03 100.0 2020-03-04 120.0 2020-03-05 180.0 2020-03-06 160.0 2020-03-07 130.0 Name: PageViews, dtype: float64
You can see that we get NaN
in the first two rows because we cannot calculate the rolling minimum as there are no preceding values to make the three-day window complete. The third row is 100 which is the minimum of the pageviews over the three-day window containing 100, 120, and 180.
Rolling min of multiple columns
If you apply the above function directly on a dataframe, it will compute the rolling minimum for all the numerical columns in the dataframe. For example, let’s get the 3-day rolling minimum over df
# 3-day rolling minimum of entire dataframe df df.rolling(3).min()
Output:
PageViews Revenue 2020-03-01 NaN NaN 2020-03-02 NaN NaN 2020-03-03 100.0 10.0 2020-03-04 120.0 12.0 2020-03-05 180.0 12.0 2020-03-06 160.0 20.0 2020-03-07 130.0 14.0
You can see that we get the rolling minimum for both the columns.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.