Pandas Series cumsum() Function:
Pandas cumsum example: The cumsum() function of Pandas Series computes the cumulative sum over a DataFrame or Series axis and provides a DataFrame or Series of the same size including the cumulative sum.
Syntax:
Series.cumsum(axis=None, skipna=True)
Parameters
axis: This is optional. It indicates 0 or “index”, 1 or “columns.” Cumulative sums are computed for each column if the value is 0 or ‘index’. Cumulative sums are calculated for each row if 1 or ‘columns’ is specified. 0 is the default.
skipna: This is optional. When determining the result, specify True to exclude NA/null values. True is the default value.
Return Value:
The cumulative sum of a scalar or a Series is returned by the cumsum() function.
- Python Pandas DataFrame ne() Function
- Python Pandas Series std() Function
- Python Pandas DataFrame ge() Function
Pandas Series cumsum() Function in Python
Example1
cumsum(): Here, the cumsum() function returns the cumulative sum of values in a given series.
Approach:
- Import pandas module using the import keyword.
- Pass some random list as an argument to the Series() function of the pandas module to create a series.
- Store it in a variable.
- Print the above-given series.
- Apply cumsum() function on the given series to get the cumulative sum values of all the elements of the given series and print the result.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword. import pandas as pd # Pass some random list as an argument to the Series() function # of the pandas module to create a series. # Store it in a variable. gvn_series = pd.Series([3, 3, 10, 5, 4, 2, 3, 6]) # Print the above given series print("The given series is:") print(gvn_series) print() # Apply cumsum() function on the given series to get the # cumulative sum values of all the elements of the given series # and print the result. print("The cumulative sum values of all the elements of the given series:") print(gvn_series.cumsum())
Output:
The given series is: 0 3 1 3 2 10 3 5 4 4 5 2 6 3 7 6 dtype: int64 The cumulative sum values of all the elements of the given series: 0 3 1 6 2 16 3 21 4 25 5 27 6 30 7 36 dtype: int64
Example2
Here, the cumsum() function is used on a specified series/columns of a DataFrame.
Approach:
- Import pandas module using the import keyword.
- Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
- Store it in a variable.
- Print the given dataframe
- Get the cumulative sum values of the student_marks column of the dataframe using the cumsum() function and print the result.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword. import pandas as pd # Pass some random key-value pair(dictionary), index list as arguments to the # DataFrame() function of the pandas module to create a dataframe # Store it in a variable. data_frme = pd.DataFrame({ "student_rollno": [1, 2, 3, 4], "student_marks": [80, 35, 25, 90]}, index= ["virat", "nick" , "jessy", "sindhu"] ) # Print the given dataframe print("The given Dataframe:") print(data_frme) print() # Get the cumulative sum values of the student_marks column of the # dataframe using the cumsum() function and print the result print("The cumulative sum of the student_marks column of the dataframe:") print(data_frme['student_marks'].cumsum())
Output:
The given Dataframe: student_rollno student_marks virat 1 80 nick 2 35 jessy 3 25 sindhu 4 90 The cumulative sum of the student_marks column of the dataframe: virat 80 nick 115 jessy 140 sindhu 230 Name: student_marks, dtype: int64