w3resource

Pandas Series: cummin() function

Cumulative minimum over a Pandas DataFrame or Series axis

The cummin() function is used to get cumulative minimum over a DataFrame or Series axis.

Returns a DataFrame or Series of the same size containing the cumulative minimum.

Syntax:

Series.cummin(self, axis=None, skipna=True, *args, **kwargs)
Pandas Series cummin image

Parameters:

Name Description Type/Default Value Required / Optional
axis The index or the name of the axis. 0 is equivalent to None or ‘index’. {0 or ‘index’, 1 or ‘columns’}
Default Value: 0
Required
skipna Exclude NA/null values. If an entire row/column is NA, the result will be NA. boolean
Default Value: True
Required
*args, **kwargs Additional keywords have no effect but might be accepted for compatibility with NumPy. Required

Returns: scalar or Series

Example - Series:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, np.nan, 7, -3, 0])
s

Output:

0    2.0
1    NaN
2    7.0
3   -3.0
4    0.0
dtype: float64
Pandas Series cummin image

Example - By default, NA values are ignored:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, np.nan, 7, -3, 0])
s.cummin()

Output:

0    2.0
1    NaN
2    2.0
3   -3.0
4   -3.0
dtype: float64

Example - To include NA values in the operation, use skipna=False:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, np.nan, 7, -3, 0])
s.cummin(skipna=False)

Output:

0    2.0
1    NaN
2    NaN
3    NaN
4    NaN
dtype: float64

Example - DataFrame:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([[2.0, 3.0],
                   [4.0, np.nan],
                   [2.0, 0.0]],
                   columns=list('XY'))
df

Output:

   X	Y
0	2.0	3.0
1	4.0	NaN
2	2.0	0.0

Example - By default, iterates over rows and finds the minimum in each column. This is equivalent to axis=None or axis='index':

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([[2.0, 3.0],
                   [4.0, np.nan],
                   [2.0, 0.0]],
                   columns=list('XY'))
df.cummin()

Output:

   X	Y
0	2.0	3.0
1	2.0	NaN
2	2.0	0.0

Example - To iterate over columns and find the minimum in each row, use axis=1:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([[2.0, 3.0],
                   [4.0, np.nan],
                   [2.0, 0.0]],
                   columns=list('XY'))
df.cummin(axis=1)

Output:

   X	Y
0	2.0	2.0
1	4.0	NaN
2	2.0	0.0

Previous: Cumulative maximum over a Pandas DataFrame or Series axis
Next: Cumulative product of a Pandas series



Follow us on Facebook and Twitter for latest update.