Questions tagged [cumsum]

Cumsum is a MatLab, NumPy, Pandas and R function that returns the cumulative sum along different dimensions of an array.

Cumsum is a MatLab, NumPy, Pandas and R function that returns the cumulative sum along different dimensions of an array.

799 questions
147
votes
8 answers

Pandas groupby cumulative sum

I would like to add a cumulative sum column to my Pandas dataframe so that: name day no Jack Monday 10 Jack Tuesday 20 Jack Tuesday 10 Jack Wednesday 50 Jill Monday 40 Jill Wednesday 110 becomes: Jack | Monday | 10 |…
kc2819
  • 1,491
  • 2
  • 10
  • 5
61
votes
5 answers

Calculate cumulative sum (cumsum) by group

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…
Rock
  • 2,827
  • 8
  • 35
  • 47
52
votes
4 answers

How to groupby consecutive values in pandas DataFrame

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]
Bryan Fok
  • 3,277
  • 2
  • 31
  • 59
44
votes
1 answer

Cumsum as a new column in an existing Pandas dataframe

I have a pandas dataframe defined as: A B SUM_C 1 1 10 1 2 20 I would like to do a cumulative sum of SUM_C and add it as a new column to the same dataframe. In other words, my end goal is to have a dataframe that looks…
user1124702
  • 1,015
  • 4
  • 12
  • 22
42
votes
3 answers

Perform a reverse cumulative sum on a numpy array

Can anyone recommend a way to do a reverse cumulative sum on a numpy array? Where 'reverse cumulative sum' is defined as below (I welcome any corrections on the name for this procedure): if x =…
Lee
  • 29,398
  • 28
  • 117
  • 170
35
votes
2 answers

cumsum per group in dplyr

I am starting to enjoy dplyr but I got stuck on a use case. I want to be able to apply cumsum per group in a dataframe with the package but I can't seem to get it right. For a demo dataframe I've generated the following data: set.seed(123) len =…
cantdutchthis
  • 31,949
  • 17
  • 74
  • 114
31
votes
5 answers

Efficient summation in Python

I am trying to efficiently compute a summation of a summation in Python: WolframAlpha is able to compute it too a high n value: sum of sum. I have two approaches: a for loop method and an np.sum method. I thought the np.sum approach would be…
Adam
  • 433
  • 5
  • 9
31
votes
1 answer

Pandas dataframe - running sum with reset

I want to calculate the running sum in a given column(without using loops, of course). The caveat is that I have this other column that specifies when to reset the running sum to the value present in that row. Best explained by the following…
Baron Yugovich
  • 3,843
  • 12
  • 48
  • 76
28
votes
3 answers

How to compute cumulative sum of previous N rows in pandas?

I am working with pandas, but I don't have so much experience. I have the following DataFrame: A 0 NaN 1 0.00 2 0.00 3 3.33 4 10.21 5 6.67 6 7.00 7 8.27 8 6.07 9 2.17 10 3.38 11 …
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
26
votes
5 answers

What is the inverse of the numpy cumsum function?

If I have z = cumsum( [ 0, 1, 2, 6, 9 ] ), which gives me z = [ 0, 1, 3, 9, 18 ], how can I get back to the original array [ 0, 1, 2, 6, 9 ] ?
scottlittle
  • 18,866
  • 8
  • 51
  • 70
25
votes
7 answers

Compute the cumulative sum of a list until a zero appears

I have a (long) list in which zeros and ones appear at random: list_a = [1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1] I want to get the list_b sum of the list up to where 0 appears where 0 appears, retain 0 in the list list_b = [1, 2, 3, 0, 1, 2, 0, 1, 0,…
22
votes
5 answers

Cumsum reset at NaN

If I have a pandas.core.series.Series named ts of either 1's or NaN's like this: 3382 NaN 3381 NaN ... 3369 NaN 3368 NaN ... 15 1 10 NaN 11 1 12 1 13 1 9 NaN 8 NaN 7 NaN 6 NaN 3 NaN 4 1 5 1 2 …
working4coins
  • 1,997
  • 3
  • 22
  • 30
20
votes
5 answers

Conditional cumsum with reset

I have a data frame, the data frame is already sorted as needed, but now I will like to "slice it" in groups. This groups should have a max cumulative value of 10. When the cumulative value is > 10, it should reset the cumulative sum and start over…
Iair Kleiman
  • 221
  • 2
  • 8
20
votes
2 answers

generalized cumulative functions in NumPy/SciPy?

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,…
Cam.Davidson.Pilon
  • 1,606
  • 1
  • 17
  • 31
18
votes
3 answers

Calculating cumulative returns with pandas dataframe

I have this dataframe Poloniex_DOGE_BTC Poloniex_XMR_BTC Daily_rets perc_ret 172 0.006085 -0.000839 0.003309 0 173 0.006229 0.002111 0.005135 0 174 0.000000 -0.001651 0.004203 0 175 0.000000 0.007743 0.005313 …
David Hancock
  • 1,063
  • 4
  • 16
  • 28
1
2 3
53 54