简体   繁体   中英

Apply function on cumulative values of pandas series

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.

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

[Follow up to @Ffisegydd's answer]

Update for pandas == 1.0.5

df['example'] = df['data'].expanding().apply(sum_)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM