Python Pandas Series cummin() Function

Pandas Series cummin() Function:

Cumulative minimum is computed over a DataFrame or Series axis by the Pandas Series cummin() function, which returns a DataFrame or Series of the same size including the cumulative minimum.

Syntax:

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

Parameters

axis: This is optional. It indicates 0 or “index”, 1 or “columns.” Cumulative minimums are generated for each column if the value is 0 or ‘index’. Cumulative minimums are generated for each row if 1 or ‘columns’ is selected. 0 is the default.

skipna: This is optional. When determining the result, specify True to exclude NA/null values. True is the default value.

Return Value:

The cumulative minimum of scalar or a Series is returned by the cummin() function.

Pandas Series cummin() Function in Python

Example1

Here, the cummin() function returns the cumulative minimum of values in a given series.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random list as an argument to the Series() function of the pandas module to create a series.
  • Store it in a variable.
  • Print the above-given series.
  • Apply cummax() function on the given series to get the cumulative minimum values of all the elements of the given series and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random list as an argument to the Series() function
# of the pandas module to create a series.
# Store it in a variable.
gvn_series = pd.Series([12, 40, 10, 5, 15, 40, 12, 12])
# Print the above given series
print("The given series is:")
print(gvn_series)
print()
# Apply cummin() function on the given series to get the 
# cumulative minimum of values of all the elements of the given series
# and print the result.
print("The cumulative minimum of values of all the elements of the given series:")
print(gvn_series.cummin())

Output:

The given series is:
0    12
1    40
2    10
3     5
4    15
5    40
6    12
7    12
dtype: int64

The cumulative minimum of values of all the elements of the given series:
0    12
1    12
2    10
3     5
4     5
5     5
6     5
7     5
dtype: int64

Example2

Here, the cummin() function is used on a specified series/columns of a DataFrame.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe
  • Get the cumulative minimum of values of the student_marks column of the dataframe using the cummin() function and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "student_rollno": [1, 2, 3, 4],
  "student_marks": [80, 35, 25, 90]},
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Get the cumulative minimum of values of the student_marks column of the 
# dataframe using the cummin() function and print the result
print("The cumulative minimum of values of the student_marks column of the dataframe:")
print(data_frme['student_marks'].cummin())

Output:

The given Dataframe:
        student_rollno  student_marks
virat                1             80
nick                 2             35
jessy                3             25
sindhu               4             90

The cumulative minimum of values of the student_marks column of the dataframe:
virat     80
nick      35
jessy     25
sindhu    25
Name: student_marks, dtype: int64