How to obtain the cumulative minimum over a pandas dataframe axis
Overview
The cummin() function of a DataFrame object is used to obtain the cumulative minimum over its axis.
Note: Axis here represents the rows and columns of the DataFrame. An axis with a value of
'0'indicates the axes running vertically downwards across a row, while a value of'1'indicates the axes running horizontally across a column.
Syntax
DataFrame.cummin(axis=None, skipna=True, *args, **kwargs)
Syntax for the cummin() function in Pandas
Parameters
axis: This represents the name for the row ( designated as0or'index') or the column (designated as1orcolumns) axis.skipna: This takes a boolean value indicating if null values are to be excluded. This is an optional parameter.argsand**kwargs: These are keywords that have no effect but may be accepted for compatibility with NumPy. These are optional.
Return value
This function returns a series or DataFrame object showing the cumulative minimum in the axis.
Example
# A code to illustrate the cummin() function in Pandas# importing the pandas libraryimport pandas as pd# creating a dataframedf = pd.DataFrame([[5,10,4,15,3],[1,7,5,9,0.5],[3,11,13,14,12]],columns=list('ABCDE'))# printing the dataframeprint(df)# obtaining the cummulative minimum vertically across rowsprint(df.cummin(axis="index"))# obtaining the cummulative minimum horizontally over columnsprint(df.cummin(axis="columns"))
Explanation
- Line 4: We import the
pandaslibrary. - Lines 7–10: We create a DataFrame,
df. - Line 12: We print the DataFrame,
df. - Line 15: We obtain the cumulative minimum values running downwards across the rows (axis
0). We print the result to the console. - Line 18: We obtain the cumulative minimum values running horizontally across columns (axis
1). We print the result to the console.