Pandas DataFrame Expanding Method

PandasPandasBeginner
Practice Now

Introduction

In this lab, we will explore the Pandas DataFrame expanding() method. This method is part of the window functions in Pandas and is used for expanding transformations. It returns a window object for the specified operation.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL pandas(("`Pandas`")) -.-> pandas/DataSelectionGroup(["`Data Selection`"]) pandas(("`Pandas`")) -.-> pandas/DataManipulationGroup(["`Data Manipulation`"]) pandas(("`Pandas`")) -.-> pandas/DataAnalysisGroup(["`Data Analysis`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) pandas/DataSelectionGroup -.-> pandas/select_columns("`Select Columns`") pandas/DataManipulationGroup -.-> pandas/add_new_columns("`Adding New Columns`") pandas/DataAnalysisGroup -.-> pandas/basic_statistics("`Basic Statistics`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills pandas/select_columns -.-> lab-68618{{"`Pandas DataFrame Expanding Method`"}} pandas/add_new_columns -.-> lab-68618{{"`Pandas DataFrame Expanding Method`"}} pandas/basic_statistics -.-> lab-68618{{"`Pandas DataFrame Expanding Method`"}} python/lists -.-> lab-68618{{"`Pandas DataFrame Expanding Method`"}} python/tuples -.-> lab-68618{{"`Pandas DataFrame Expanding Method`"}} python/dictionaries -.-> lab-68618{{"`Pandas DataFrame Expanding Method`"}} python/importing_modules -.-> lab-68618{{"`Pandas DataFrame Expanding Method`"}} python/numerical_computing -.-> lab-68618{{"`Pandas DataFrame Expanding Method`"}} python/data_analysis -.-> lab-68618{{"`Pandas DataFrame Expanding Method`"}} python/build_in_functions -.-> lab-68618{{"`Pandas DataFrame Expanding Method`"}} end

Import the necessary libraries

First, we need to import the required libraries, namely Pandas. We will use the pd alias to refer to the Pandas library.

import pandas as pd

Create the DataFrame

Next, we will create a DataFrame to work with. In this example, we will use a DataFrame with columns 'A' and 'B'.

df = pd.DataFrame({"A": [1, 2, 3], "B": [1, 1, 1]})

Apply the expanding method to calculate the cumulative sum

Now, let's apply the expanding() method to calculate the cumulative sum of the entire DataFrame.

df_expanding_sum = df.expanding().sum()
print(df_expanding_sum)

Store the result in a new column

We can also store the result of the expanding calculation in a new column of the DataFrame. In this example, we will store the cumulative sum of column 'A' in a new column 'result'.

df['result'] = df['A'].expanding().sum()
print(df)

Calculate the cumulative sum along the row axis

Furthermore, we can calculate the cumulative sum of the entire DataFrame along the row axis.

df_expanding_sum_row = df.expanding(axis=1).sum()
print(df_expanding_sum_row)

Specify the minimum number of observations required

We can also specify the minimum number of observations required for the expanding calculation to have a value. This can be done using the min_periods parameter. In this example, we will set min_periods=2.

df_expanding_sum_min = df.expanding(min_periods=2).sum()
print(df_expanding_sum_min)

Summary

In this lab, we learned about the expanding() method in Pandas DataFrame. We applied this method to calculate the cumulative sum of a DataFrame, store the result in a new column, calculate the cumulative sum along the row axis, and specify a minimum number of observations for the expanding calculation. The expanding() method is useful for exploring the cumulative sum of data over time.

Other Pandas Tutorials you may like