Pandas Tutorial Pandas References

Pandas Series - cummin() function



The Pandas Series cummin() function computes cumulative minimum over a DataFrame or Series axis and returns a DataFrame or Series of the same size containing the cumulative minimum.

Syntax

Series.cummin(axis=None, skipna=True)

Parameters

axis Optional. Specify {0 or 'index', 1 or 'columns'}. If 0 or 'index', cumulative minimums are generated for each column. If 1 or 'columns', cumulative minimums are generated for each row. Default: 0
skipna Optional. Specify True to exclude NA/null values when computing the result. Default is True.

Return Value

Return cumulative minimum scalar or Series.

Example: using cummin() on a Series

In the example below, the cummin() function is used to get the cumulative minimum of values of a given series.

import pandas as pd
import numpy as np

x = pd.Series([10, 11, 12, 9, 13, 12, 10])

print("The Series contains:")
print(x)

#cumulative minimum of values in the series
print("\nx.cummin() returns:")
print(x.cummin())

The output of the above code will be:

The Series contains:
0    10
1    11
2    12
3     9
4    13
5    12
6    10
dtype: int64

x.cummin() returns:
0    10
1    10
2    10
3     9
4     9
5     9
6     9
dtype: int64

Example: using cummin() on selected series in a DataFrame

Similarly, the cummin() function can be applied on selected series/column of a given DataFrame. Consider the following example.

import pandas as pd
import numpy as np

info = pd.DataFrame({
  "Salary": [25, 24, 30, 28, 25],
  "Bonus": [10, 8, 9, np.nan, 9],
  "Others": [5, 4, 7, 5, 8]},
  index= ["2015", "2016", "2017", "2018", "2019"]
)

print("The DataFrame is:")
print(info,"\n")

#cumulative minimum on 'Salary' series
print("info['Salary'].cummin() returns:")
print(info['Salary'].cummin(),"\n")

The output of the above code will be:

The DataFrame is:
      Salary  Bonus  Others
2015      25   10.0       5
2016      24    8.0       4
2017      30    9.0       7
2018      28    NaN       5
2019      25    9.0       8 

info['Salary'].cummin() returns:
2015    25
2016    24
2017    24
2018    24
2019    24
Name: Salary, dtype: int64 

❮ Pandas Series - Functions

5