Pandas DataFrame.cummin: Cumulative Minimum of DataFrame Elements


Pandas DataFrame.cummin

The DataFrame.cummin method in pandas computes the cumulative minimum of DataFrame elements along a specified axis. It can handle missing values (NaN) and offers options to skip them.


Syntax

The syntax for DataFrame.cummin is:

DataFrame.cummin(axis=None, skipna=True, *args, **kwargs)

Here, DataFrame refers to the pandas DataFrame on which the cumulative minimum operation is applied.


Parameters

ParameterDescription
axisSpecifies the axis along which the cumulative minimum is computed. Use 0 or 'index' for columns, and 1 or 'columns' for rows. Defaults to None.
skipnaIf True, skips NaN values while performing the operation. Defaults to True.
*args and **kwargsAdditional positional and keyword arguments to be passed to the function.

Returns

A DataFrame with the cumulative minimum computed along the specified axis.


Examples

Computing the Cumulative Minimum of a DataFrame

This example demonstrates how to compute the cumulative minimum along columns in a DataFrame.

Python Program

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({
    'A': [3, 2, 1, 4],
    'B': [10, 20, 15, 5]
})

# Compute the cumulative minimum along columns
result = df.cummin(axis=0)
print(result)

Output

   A   B
0  3  10
1  2  10
2  1  10
3  1   5

Handling Missing Values in a DataFrame

This example shows how DataFrame.cummin behaves when the DataFrame contains missing values (NaN).

Python Program

import pandas as pd

# Create a DataFrame with missing values
df = pd.DataFrame({
    'A': [3, None, 1, 4],
    'B': [10, 20, None, 5]
})

# Compute the cumulative minimum while skipping NaN values
result = df.cummin(axis=0)
print(result)

Output

     A     B
0  3.0  10.0
1  NaN  10.0
2  1.0  10.0
3  1.0   5.0

Computing Cumulative Minimum Along Rows in a DataFrame

This example demonstrates how to compute the cumulative minimum along rows in a DataFrame.

Python Program

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({
    'A': [3, 2, 1, 4],
    'B': [10, 20, 15, 5]
})

# Compute the cumulative minimum along rows
result = df.cummin(axis=1)
print(result)

Output

   A   B
0  3   3
1  2   2
2  1   1
3  4   4

Summary

In this tutorial, we explored the DataFrame.cummin method in pandas. Key takeaways include:

  • Using cummin to compute the cumulative minimum of DataFrame elements.
  • Handling missing values with the skipna parameter.
  • Applying the cumulative minimum along rows or columns using the axis parameter.

Python Libraries