Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply function on cumulative values of pandas series

Tags:

python

pandas

Is there an equivalent of rolling_apply in pandas that applies function to the cumulative values of a series rather than the rolling values? I realize cumsum, cumprod, cummax, and cummin exist, but I'd like to apply a custom function.

like image 762
user1507844 Avatar asked Mar 13 '15 13:03

user1507844


People also ask

Which function is used for cumulative product in pandas?

Pandas DataFrame cumprod() Method The cumprod() method returns a DataFrame with the cumulative product for each row.

How do you do the cumulative sum of a panda?

The cumsum() method returns a DataFrame with the cumulative sum for each row. The cumsum() method goes through the values in the DataFrame, from the top, row by row, adding the values with the value from the previous row, ending up with a DataFrame where the last row contains the sum of all values for each column.

What is the difference between apply and Applymap in pandas?

apply() is used to apply a function along an axis of the DataFrame or on values of Series. applymap() is used to apply a function to a DataFrame elementwise. map() is used to substitute each value in a Series with another value.


1 Answers

You can use pd.expanding_apply. Below is a simple example which only really does a cumulative sum, but you could write whatever function you wanted for it.

import pandas as pd

df = pd.DataFrame({'data':[10*i for i in range(0,10)]})

def sum_(x):
    return sum(x)


df['example'] = pd.expanding_apply(df['data'], sum_)

print(df)

#   data  example
#0     0        0
#1    10       10
#2    20       30
#3    30       60
#4    40      100
#5    50      150
#6    60      210
#7    70      280
#8    80      360
#9    90      450
like image 144
Ffisegydd Avatar answered Oct 08 '22 16:10

Ffisegydd