The cumsum()
function in Pandas is used to compute the cumulative sum of the values in a column or series. The cumulative sum is the sum of all the values in a series up to that point, including the current value.
For example, if the series is [1, 2, 3, 4, 5], the cumulative sum at each point would be [1, 3, 6, 10, 15].
Here's an example of using the cumsum()
function in Pandas:
import pandas as pd
# create a sample series
s = pd.Series([1, 2, 3, 4, 5])
# compute the cumulative sum of the series
s_cumsum = s.cumsum()
# display the result
print(s_cumsum)
This will compute the cumulative sum of the series and return a new series with the same index as the original series.
The output will be:
0 1
1 3
2 6
3 10
4 15
dtype: int64
You can also use the cumsum()
function on a dataframe to compute the cumulative sum of the values in one or more columns. For example:
# create a sample dataframe
df = pd.DataFrame({"A": [1, 2, 3, 4, 5],
"B": [2, 3, 4, 5, 6],
"C": [3, 4, 5, 6, 7]})
# compute the cumulative sum of the A and B columns
df_cumsum = df[["A", "B"]].cumsum()
# display the result
print(df_cumsum)
This will compute the cumulative sum of the A
and B
columns in the dataframe and return a new dataframe with the same index as the original dataframe. The output will be:
A B
0 1 2
1 3 5
2 6 9
3 10 14
4 15 20
As you can see, the cumsum()
function is useful for computing the cumulative sum of a series or dataframe, which can be useful for a variety of analysis and visualization tasks.
Related tutorials curated for you
How to groupby mean in Pnadas
What is nlargest() in Pandas?
How to shuffle data in Pandas
How to convert Pandas timestamp to datetime
How to change the order of columns in Pandas
How to use str.contains() in Pandas
How to reorder columns in Pandas
How to split a Pandas DataFrame by a column value
How to join two DataFrames in Pandas
How to make a crosstab in Pandas
How to apply a function to multiple columns in Pandas
What is isna() in Pandas?