Pandas DataFrame.cumsum: Cumulative Sum of DataFrame Elements


Pandas DataFrame.cumsum

The DataFrame.cumsum method in pandas computes the cumulative sum of DataFrame elements along a specified axis, optionally skipping missing values (NaN).


Syntax

The syntax for DataFrame.cumsum is:

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

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


Parameters

ParameterDescription
axisSpecifies the axis along which the cumulative sum is computed. Use 0 or 'index' for columns, and 1 or 'columns' for rows. Defaults to None (applies to all elements).
skipnaIf True, skips NaN values while computing the cumulative sum. If False, NaN values propagate. Defaults to True.
*args, **kwargsAdditional arguments to pass to the method.

Returns

A DataFrame containing the cumulative sum of elements along the specified axis.


Examples

Computing Cumulative Sum Along DataFrame Columns

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

Python Program

import pandas as pd

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

# Compute cumulative sum along columns
result = df.cumsum()
print(result)

Output

    A   B
0   1  10
1   3  30
2   6  60
3  10 100

Computing Cumulative Sum Along DataFrame Rows

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

Python Program

import pandas as pd

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

# Compute cumulative sum along rows
result = df.cumsum(axis=1)
print(result)

Output

    A   B   C
0   1   5  12
1   2   7  15
2   3   9  18

Handling Missing Values in a DataFrame

This example demonstrates how DataFrame.cumsum handles NaN values.

Python Program

import pandas as pd

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

# Compute cumulative sum, skipping NaN values
result = df.cumsum()
print(result)

Output

      A     B
0   1.0  10.0
1   1.0  30.0
2   4.0  30.0
3   8.0  70.0

Computing Cumulative Sum Without Skipping NaN

This example demonstrates how to compute the cumulative sum without skipping NaN values.

Python Program

import pandas as pd

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

# Compute cumulative sum, propagating NaN values
result = df.cumsum(skipna=False)
print(result)

Output

      A     B
0   1.0  10.0
1   NaN  30.0
2   NaN   NaN
3   NaN   NaN

Summary

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

  • Using cumsum to compute cumulative sums along rows or columns.
  • Handling NaN values with the skipna parameter.
  • Applying cumulative sums along a specific axis using the axis parameter.

Python Libraries