Pandas DataFrame - cummin() function
The Pandas DataFrame 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
DataFrame.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 of Series or DataFrame.
Example: using cummin() column-wise on whole DataFrame
In the example below, a DataFrame info is created. The cummin() function is used to get the cumulative minimum of each column.
import pandas as pd import numpy as np info = pd.DataFrame({ "Salary": [25, 24, 30, 28, 25], "Bonus": [10, 8, 9, np.nan, 9]}, index= ["2015", "2016", "2017", "2018", "2019"] ) #displaying the dataframe print(info,"\n") #displaying the cumulative minimum print("info.cummin() returns:") print(info.cummin(),"\n") #using skipna=False print("info.cummin(skipna=False) returns:") print(info.cummin(skipna=False))
The output of the above code will be:
Salary Bonus 2015 25 10.0 2016 24 8.0 2017 30 9.0 2018 28 NaN 2019 25 9.0 info.cummin() returns: Salary Bonus 2015 25 10.0 2016 24 8.0 2017 24 8.0 2018 24 NaN 2019 24 8.0 info.cummin(skipna=False) returns: Salary Bonus 2015 25 10.0 2016 24 8.0 2017 24 8.0 2018 24 NaN 2019 24 NaN
Example: using cummin() row-wise on whole DataFrame
To get the row-wise cumulative minimum, the axis parameter can be set to 1.
import pandas as pd import numpy as np info = pd.DataFrame({ "2016": [25, 24, 30, 28, 25], "2017": [18, 20, 25, np.nan, 28], "2018": [25, 24, 25, 30, 25]}, index= ["P1", "P2", "P3", "P4", "P5"] ) #displaying the dataframe print(info,"\n") #displaying the cumulative minimum print("info.cummin(axis=1) returns:") print(info.cummin(axis=1),"\n") #using skipna=False print("info.cummin(axis=1, skipna=False) returns:") print(info.cummin(axis=1, skipna=False))
The output of the above code will be:
2016 2017 2018 P1 25 18.0 25 P2 24 20.0 24 P3 30 25.0 25 P4 28 NaN 30 P5 25 28.0 25 info.cummin(axis=1) returns: 2016 2017 2018 P1 25.0 18.0 18.0 P2 24.0 20.0 20.0 P3 30.0 25.0 25.0 P4 28.0 NaN 28.0 P5 25.0 25.0 25.0 info.cummin(axis=1, skipna=False) returns: 2016 2017 2018 P1 25.0 18.0 18.0 P2 24.0 20.0 20.0 P3 30.0 25.0 25.0 P4 28.0 NaN NaN P5 25.0 25.0 25.0
Example: using cummin() on selected column
Instead of whole DataFrame, the cummin() function can be applied on selected columns. 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"] ) #displaying the dataframe print(info,"\n") #cumulative minimum on single column print("info['Salary'].cummin() returns:") print(info['Salary'].cummin(),"\n") #cumulative minimum on multiple column print("info[['Salary', 'Others']].cummin() returns:") print(info[['Salary', 'Others']].cummin(),"\n")
The output of the above code will be:
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 info[['Salary', 'Others']].cummin() returns: Salary Others 2015 25 5 2016 24 4 2017 24 4 2018 24 4 2019 24 4
❮ Pandas DataFrame - Functions