YouTip LogoYouTip

Pandas Series Cumsum

[![Image 1: Pandas Common Functions](#) Pandas Common Functions](#) * * * `Series.cumsum()` is a function in Pandas used to calculate the cumulative sum of a Series. The cumulative sum means that starting from the first element, the value at each position is equal to the sum of all elements before that position (including the current element). Cumulative sum is frequently used in financial data analysis (such as calculating cumulative returns), sales analysis (such as calculating cumulative sales), and time series analysis. * * * ## Basic Syntax and Parameters `cumsum()` is a member function of the Series object, called directly via the dot operator. ### Syntax Series.cumsum(axis=None, skipna=True, dtype=None, out=None, **kwargs) ### Parameter Description | Parameter | Type | Description | Default | | --- | --- | --- | --- | | axis | int | Specifies the axis. Series only has one column; this parameter exists mainly for compatibility with DataFrame. | None | | skipna | bool | If True, skips NaN values during calculation; if False, encountering NaN will interrupt the cumulative calculation. | True | | dtype | dtype | Specifies the output data type. | None | | out | ndarray | Array used to store the result, usually does not need to be set. | None | ### Return Value * **Return Type**: `Series` * **Description**: Returns a new Series where the value at each position is the cumulative sum. * * * ## Examples Let's thoroughly master the usage of `Series.cumsum()` through a series of examples ranging from simple to complex. ### Example 1: Basic Usage - Calculating Cumulative Sales Cumulative sum refers to the total of all numerical values up to the current position. ## Example import pandas as pd # Create a Series containing daily sales daily_sales = pd.Series([1000,1500,1200,1800,2000,900,1600]) print("Daily Sales (Yuan):") print(daily_sales) print() # Calculate cumulative sales cumulative_sales = daily_sales.cumsum() print("Cumulative Sales (Yuan):") print(cumulative_sales) print() print("Analysis:") print("- Day 1 cumulative: 1000") print("- Day 2 cumulative: 1000 + 1500 = 2500") print("- Day 3 cumulative: 1000 + 1500 + 1200 = 3700") print("And so on...") **Output:** Daily Sales (Yuan):0 10001 15002 12003 18004 20005 9006 1600 dtype: int64 Cumulative Sales (Yuan):0 10001 25002 37003 55004 75005 84006 10000 dtype: int64 Analysis:- Day 1 cumulative sales is the sales of that day- Day 2 cumulative sales = Day 1 + Day 2 sales- And so on... **Code Analysis:** * Cumulative sum at position 1 = 1000 * Cumulative sum at position 2 = 1000 + 1500 = 2500 * Cumulative sum at position 3 = 1000 + 1500 + 1200 = 3700 * And so on... ### Example 2: Handling Data with Missing Values The `skipna` parameter determines how missing values are handled. ## Example import pandas as pd import numpy as np # Create a Series containing missing values data_with_nan = pd.Series([10,20, np.nan,30,40]) print("Original Data (with missing values):") print(data_with_nan) print() # Default skipna=True, skip NaN when calculating cumulative sum cumulative_skipna = data_with_nan.cumsum() print("Cumulative sum when skipna=True (default):") print(cumulative_skipna) print() # Set skipna=False, NaN position will interrupt cumulative calculation cumulative_no_skipna
← Pandas Series Str UpperPandas Series Corr β†’