search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas DataFrame | expanding method

schedule Aug 12, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas DataFrame.expanding(~) method is used to compute cumulative statistics.

Parameters

1. min_periods | int | optional

The minimum number of values in a window to compute the statistic. If the number of observations in a window is less than min_periods, then a NaN is returned for that window. By default, min_periods=1.

NOTE

A window is essentially a sequence of numbers (observations) from which a statistic is computed.

2. center | boolean | optional

  • If True, then the observation is set to the center of the window.

  • If False, then the observation is set to the right of the window.

By default, center=False. Consult examples below for clarification.

3. axis | int or string | optional

Whether to perform expansion row-wise or column-wise:

Axis

Description

"index" or 0

Expand column-wise.

"columns" or 1

Expand row-wise.

Return Value

A Window object that will be subsequently used to compute some cumulative statistics.

Examples

Computing the cumulative sum

Consider the following DataFrame:

df = pd.DataFrame({"A":[2,4,8],"B":[4,5,6]})
df
A B
0 2 4
1 4 5
2 8 6

Column-wise

To compute the cumulative sum column-wise:

df.expanding().sum()
A B
0 2.0 4.0
1 6.0 9.0
2 14.0 15.0

Row-wise

To compute the cumulative sum row-wise:

df.expanding(axis=1).sum()
A B
0 2.0 6.0
1 4.0 9.0
2 8.0 14.0
WARNING

This example is just to demonstrate how to use the method. Pandas has a method DataFrame.cumsum(~) that directly computes the cumulative sums.

Specifying min_periods

Consider the same DataFrame as above:

df = pd.DataFrame({"A":[2,4,8],"B":[4,5,6]})
df
A B
0 2 4
1 4 5
2 8 6

Calling expanding(~) with min_periods=2:

df.expanding(2).sum()
A B
0 NaN NaN
1 6.0 9.0
2 14.0 15.0

Here, we get NaN for the first row because the very first cumulative sum is computed using only 1 value, which is lower than the specified minimum of 2.

Specifying the center parameter

Consider the following DataFrame:

df = pd.DataFrame({"A": [2,4,8,16,25]})
df
A
0 2
1 4
2 8
3 16
4 25

Calling expanding(~) with the center parameter set to True:

df.expanding(center=True).sum()
A
0 14.0
1 30.0
2 55.0
3 53.0
4 49.0

The size of the initial selection of values is determined as follows:

ceil(num_of_rows/2) = 3

Here's a breakdown of how the numbers are computed:

A[0]: sum(2, 4, 8) = 14
A[1]: sum(2, 4, 8, 16) = 30
A[2]: sum(2, 4, 8, 16, 25) = 55
A[3]: sum(4, 8, 16, 25) = 53
A[4]: sum(8, 16, 25) = 49
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
1
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!