Pandas DataFrame.cummax: Cumulative Maximum in a DataFrame


Pandas DataFrame.cummax

The DataFrame.cummax method in pandas is used to compute the cumulative maximum of DataFrame values along a specified axis. This method is useful for analyzing trends where the maximum value needs to be tracked incrementally.


Syntax

The syntax for DataFrame.cummax is:

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

Here, DataFrame refers to the pandas DataFrame on which the cumulative maximum operation is performed.


Parameters

ParameterDescription
axisThe axis along which the cumulative maximum is computed. Use 0 or 'index' for columns, and 1 or 'columns' for rows. Defaults to None.
skipnaExcludes NaN values when computing the cumulative maximum. If False, NaN values propagate. Defaults to True.
*args, **kwargsAdditional arguments and keyword arguments to pass to the method.

Returns

A DataFrame with the cumulative maximum values computed along the specified axis.


Examples

Computing Cumulative Maximum Along DataFrame Columns

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

Python Program

import pandas as pd

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

# Compute cumulative maximum along columns
result = df.cummax(axis=0)
print(result)

Output

   A  B
0  1  7
1  3  7
2  3  7
3  5  7

Computing Cumulative Maximum Along DataFrame Rows

This example shows how to compute the cumulative maximum along rows in a DataFrame.

Python Program

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({
    'A': [1, 3, 2],
    'B': [7, 2, 5],
    'C': [4, 6, 8]
})

# Compute cumulative maximum along rows
result = df.cummax(axis=1)
print(result)

Output

   A  B  C
0  1  7  7
1  3  3  6
2  2  5  8

Handling Missing Values in a DataFrame

This example demonstrates how DataFrame.cummax handles missing values (NaN) when computing the cumulative maximum.

Python Program

import pandas as pd
import numpy as np

# Create a DataFrame with missing values
df = pd.DataFrame({
    'A': [1, np.nan, 3, 5],
    'B': [7, 2, np.nan, 1]
})

# Compute cumulative maximum while skipping NaN values
result = df.cummax()
print(result)

Output

     A    B
0  1.0  7.0
1  1.0  7.0
2  3.0  7.0
3  5.0  7.0

Summary

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

  • Using cummax to compute cumulative maximum values along columns or rows in a DataFrame.
  • Handling missing values during cumulative maximum computation.
  • Applying the axis parameter to control the direction of computation.

Python Libraries