Signup/Sign In

Pandas DataFrame expanding() Method

In this tutorial, we will learn the Python pandas DataFrame.expanding() method. This is one of the window methods of pandas and it provides expanding transformations. It returns a window sub-classed for the particular operation.

The below shows the syntax of the DataFrame.expanding() method.

Syntax

DataFrame.expanding(min_periods=1, center=None, axis=0)

Parameters

min_periods: int, default 1. The minimum number of observations in the window required to have a value (otherwise result is NA).

center: bool, default False. Set the labels at the center of the window.

axis: int or str, default 0

Example 1: Expanding the DataFrame

In the below example, the DataFrame.expanding() method calculated the cumulative sum of the entire DataFrame.

import pandas as pd
df = pd.DataFrame({"A": [1, 2, 3],"B": [1, 1, 1]})
print("---The DataFrame is---")
print(df)
print("------Output of the function is-------")
print(df.expanding().sum())

Once we run the program we will get the following output.


---The DataFrame is---
A B
0 1 1
1 2 1
2 3 1
------Output of the method is-------
A B
0 1.0 1.0
1 3.0 2.0
2 6.0 3.0

Example 2: Expanding the DataFrame

In the below example, the DataFrame.expanding() method calculated the cumulative sum of the selected column in the DataFrame and store the result in the other column.

import pandas as pd
df = pd.DataFrame({"A": [1, 2, 3],"B": [1, 1, 1]})
print("---The DataFrame is---")
print(df)
print("------Output of the function is-------")
df["result"]=df.A.expanding().sum()
print(df)

Once we run the program we will get the following output.


---The DataFrame is---
A B
0 1 1
1 2 1
2 3 1
------Output of the method is-------
A B result
0 1 1 1.0
1 2 1 3.0
2 3 1 6.0

Example 3: Expanding the DataFrame

In the below example, the DataFrame.expanding() method calculated the cumulative sum of the entire DataFrame along the row axis.

import pandas as pd
df = pd.DataFrame({"A": [1, 2, 3],"B": [1, 1, 1]})
print("---The DataFrame is---")
print(df)
print("------Output of the function is-------")
print(df.expanding(axis=1).sum())

Once we run the program we will get the following output.


---The DataFrame is---
A B
0 1 1
1 2 1
2 3 1
------Output of the method is-------
A B
0 1.0 2.0
1 2.0 3.0
2 3.0 4.0

Example 4: Expanding the DataFrame with min_periods

In the below example the DataFrame.expanding() method calculated the cumulative sum of the entire DataFrame with min_periods=2.

import pandas as pd
df = pd.DataFrame({"A": [1, 2, 3],"B": [1, 1, 1]})
print("---The DataFrame is---")
print(df)
print("------Output of the function is-------")
print(df.expanding(min_periods=2).sum())

Once we run the program we will get the following output.


---The DataFrame is---
A B
0 1 1
1 2 1
2 3 1
------Output of the method is-------
A B
0 NaN NaN
1 3.0 2.0
2 6.0 3.0

Conclusion:

In this tutorial, we learned the pandas DataFrame.expanding() method. We solved different examples by applying this method to the DataFrames. We applied this method to the DataFrame with mean, median, variance, covariance, correlation, etc., and observe the output.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.