How does the series.cummim() method work in Pandas?

PandasServer Side ProgrammingProgramming

The cummin() method in the Pandas Series constructor is used to find the cumulative minimum of the elements of a given series.

The resultant cumulative minimum object has the same length as the original series object. The parameters of the cummin() method are “axis”, “skipna” and additional keywords.

The “skipna” parameter excludes execution of missing values by default, if you want to execute those missing values too then set the skipna parameter to “False” then it includes Nan/null values also.

Example 1

# importing required packages
import pandas as pd
import numpy as np

# create a pandas Series object
series = pd.Series([9,10,5,np.nan,23,7])
print(series)

print("Cumulative minimum: ",series.cummin())

Explanation

In this example, we have created a pandas series using a python list integer values and with a Null value. After creating a series object we applied the cummin() method without changing any default parameter values.

Output

0  9.0
1 10.0
2  5.0
3  NaN
4 23.0
5  7.0
dtype: float64

Cumulative minimum:
0 9.0
1 9.0
2 5.0
3 NaN
4 5.0
5 5.0
dtype: float64

By default the cummin() method doesn’t execute the Nan values, hence the Nan value at position 3 remains the same.

Example 2

# importing required packages
import pandas as pd
import numpy as np

# create a pandas Series object
series = pd.Series([78,23,65,np.nan,92,34])
print(series)

print("Cumulative minimum: ",series.cummin(skipna=False))

Explanation

In the following example, we applied the cummin() method by setting the skipna value to False. This means it will consider the Null/Nan values while executing.

Output

0 78.0
1 23.0
2 65.0
3  NaN
4 92.0
5 34.0
dtype: float64

Cumulative minimum:
0 78.0
1 23.0
2 23.0
3  NaN
4  NaN
5  NaN
dtype: float64

The Series.cummin() method returns a Series object, and the first element of the cumulative minimum element is the same element of the original series.

raja
Updated on 09-Mar-2022 08:04:51

Advertisements