Wednesday, December 25, 2024
Google search engine
HomeLanguagesPython | Pandas Series.cummin() to find cumulative minimum of a series

Python | Pandas Series.cummin() to find cumulative minimum of a series

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Pandas Series.cummin() is used to find Cumulative minimum of a series. In cumulative minimum, the length of returned series is same as input series and every element is equal to the smaller one between current element and previous element.

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

Parameters:
axis: 0 or ‘index’ for row wise operation and 1 or ‘columns’ for column wise operation
skipna: Skips NaN addition for elements after the very next one if True.

Return type: Series

Example #1:
In this example, a series is created from a Python list. The list also contains a Null value and the skipna parameter is kept default, that is True.




# importing pandas module 
import pandas as pd 
    
# importing numpy module 
import numpy as np 
    
# making list of values 
values = [3, 4, np.nan, 7, 2, 0
    
# making series from list 
series = pd.Series(values) 
    
# calling method 
cummin = series.cummin() 
    
# display 
cummin


Output:

0    3.0
1    3.0
2    NaN
3    3.0
4    2.0
5    0.0
dtype: float64

Explanation: Cummin is comparison of current value with previous value. The first element is always equal to first of caller series.

3
3  (3<4)
NaN (Since NaN cannot be compared to integer values)
3  (3<7)
2  (2<3)
0  (0<2)

 
Example #2: Keeping skipna=False

In this example, a series is created just like in the above example. But the skipna parameter is kept False. Hence NULL values won’t be ignored and it would be compared every time on it’s occurrence.




# importing pandas module 
import pandas as pd 
    
# importing numpy module 
import numpy as np 
    
# making list of values 
values = [12, 4, 33, np.nan, 0, 1, 76, 5
    
# making series from list 
series = pd.Series(values) 
    
# calling method 
cummin = series.cummin(skipna = False
    
# display 
cummin 


Output:

0    12.0
1     4.0
2     4.0
3     NaN
4     NaN
5     NaN
6     NaN
7     NaN
dtype: float64

Explanation: Just like in the above example, minimum of current and previous values was stored at every position until NaN occurred. Since NaN compared with anything returns NaN and skipna parameter is kept False, the cumulative minimum after its occurrence is NaN due to comparison of all the values with NaN.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments