cumsum

generalized cumulative functions in NumPy/SciPy?

让人想犯罪 __ 提交于 2019-11-26 14:14:10
问题 Is there a function in numpy or scipy (or some other library) that generalizes the idea of cumsum and cumprod to arbitrary function. For example, consider the (theoretical) function cumf( func, array) func is a function that accepts two floats, and returns a float. Particular cases lambda x,y: x+y and lambda x,y: x*y are cumsum and cumprod respectively. For example, if func = lambda x,prev_x: x^2*prev_x and I apply it to: cumf(func, np.array( 1, 2, 3) ) I would like np.array( 1, 4, 9*4 ) 回答1:

Cumulative sum until maximum reached, then repeat from zero in the next row

♀尐吖头ヾ 提交于 2019-11-26 12:19:46
问题 I feel like this is a fairly easy question, but for the life of me I can\'t seem to find the answer. I have a fairly standard dataframe, and what I am trying to do is sum the a column of values until they reach some value (either that exact value or greater than it), at which point it drops a 1 into a new column (labelled keep) and restarts the summing at 0. I have a column of minutes, the differences between the minutes, a keep column, and a cumulative sum column (the example I am using is

Calculate cumulative sum within each ID (group)

半世苍凉 提交于 2019-11-26 03:58:53
问题 With data frame: df <- data.frame(id = rep(1:3, each = 5) , hour = rep(1:5, 3) , value = sample(1:15)) I want to add a cumulative sum column that matches the id : df id hour value csum 1 1 1 7 7 2 1 2 9 16 3 1 3 15 31 4 1 4 11 42 5 1 5 14 56 6 2 1 10 10 7 2 2 2 12 8 2 3 5 17 9 2 4 6 23 10 2 5 4 27 11 3 1 1 1 12 3 2 13 14 13 3 3 8 22 14 3 4 3 25 15 3 5 12 37 How can I do this efficiently? Thanks! 回答1: df$csum <- ave(df$value, df$id, FUN=cumsum) ave is the "go-to" function if you want a by

Pandas DataFrame: How to groupby consecutive values

时间秒杀一切 提交于 2019-11-25 19:40:56
I have a column in a DataFrame with values: [1, 1, -1, 1, -1, -1] How can I group them like this? [1,1] [-1] [1] [-1, -1] You can use groupby by custom Series : df = pd.DataFrame({'a': [1, 1, -1, 1, -1, -1]}) print (df) a 0 1 1 1 2 -1 3 1 4 -1 5 -1 print ((df.a != df.a.shift()).cumsum()) 0 1 1 1 2 2 3 3 4 4 5 4 Name: a, dtype: int32 for i, g in df.groupby([(df.a != df.a.shift()).cumsum()]): print (i) print (g) print (g.a.tolist()) a 0 1 1 1 [1, 1] 2 a 2 -1 [-1] 3 a 3 1 [1] 4 a 4 -1 5 -1 [-1, -1] Using groupby from itertools data from Jez from itertools import groupby [ list(group) for key,