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
16
votes
4 answers

Python pandas cumsum with reset everytime there is a 0

I have a matrix with 0s and 1s, and want to do a cumsum on each column that resets to 0 whenever a zero is observed. For example, if we have the following: df = pd.DataFrame([[0,1],[1,1],[0,1],[1,0],[1,1],[0,1]],columns = ['a','b']) print(df) a …
nanojohn
  • 572
  • 1
  • 3
  • 13
16
votes
3 answers

Cumulative sum in a matrix

I have a matrix like A= [ 1 2 4 2 3 1 3 1 2 ] and I would like to calculate its cumulative sum by row and by column, that is, I want the result to be B = [ 1 3 7 3 8 13 6 12 19 ] Any ideas of how to make this in R in a…
madness
  • 371
  • 1
  • 3
  • 14
14
votes
3 answers

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

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…
HeidelbergSlide
  • 293
  • 3
  • 13
13
votes
2 answers

Pandas group by cumsum keep columns

I have spent a few hours now trying to do a "cumulative group by sum" on a pandas dataframe. I have looked at all the stackoverflow answers and surprisingly none of them can solve my (very elementary) problem: I have a dataframe: df1 Out[8]: …
gmarais
  • 1,801
  • 4
  • 16
  • 32
12
votes
7 answers

Identifying positions of the last TRUEs in a sequence of TRUEs and FALSEs

I have a vector of TRUEs and FALSEs: x <- c(F,F,F,T,T,T,F,F,F,T,T,T,F,T,T) I'd like to elegantly (and in base) identify the position of the last TRUE before it changes to FALSE. The following works, though, it seems like it could be…
Khaynes
  • 1,976
  • 2
  • 15
  • 27
12
votes
4 answers

R: cumulative sum over rolling date range

In R, how can I calculate cumsum for a defined time period prior to the row being calculate? Prefer dplyr if possible. For example, if the period was 10 days, then the function would achieve cum_rolling10: date value cumsum …
Vlad
  • 3,058
  • 4
  • 25
  • 53
12
votes
3 answers

Conditional Cumulative Sum in R

I have a time series data frame and want to compute cumulative returns for stock symbols intra-day for a range of dates. When the symbol and/or date changes the cumulative return should reset. Any help would be appreciated. A small sample of my…
David
  • 121
  • 1
  • 1
  • 3
11
votes
1 answer

Cumsum Reset based on a condition in Pandas

My question is very similar to Cumsum within group and reset on condition in pandas and Pandas: cumsum per category based on additional condition but they don't quite get me there due to my conditional requirements. I have a data frame that looks…
thepez87
  • 221
  • 2
  • 12
11
votes
1 answer

Scipy Sparse Cumsum

Suppose I have a scipy.sparse.csr_matrix representing the values below [[0 0 1 2 0 3 0 4] [1 0 0 2 0 3 4 0]] I want to calculate the cumulative sum of non-zero values in-place, which would change the array to: [[0 0 1 3 0 6 0 10] [1 0 0 3 0 6 10…
hamster on wheels
  • 2,771
  • 17
  • 50
11
votes
4 answers

Can the cumsum function in NumPy decay while adding?

I have an array of values a = (2,3,0,0,4,3) y=0 for x in a: y = (y+x)*.95 Is there any way to use cumsum in numpy and apply the .95 decay to each row before adding the next value?
D_C_A
  • 111
  • 3
11
votes
3 answers

Numpy cumsum considering NaNs

I am looking for a succinct way to go from: a = numpy.array([1,4,1,numpy.nan,2,numpy.nan]) to: b = numpy.array([1,5,6,numpy.nan,8,numpy.nan]) The best I can do currently is: b = numpy.insert(numpy.cumsum(a[numpy.isfinite(a)]),…
Benjamin
  • 11,560
  • 13
  • 70
  • 119
10
votes
3 answers

How to count the number of occurences before a particular value in dataframe python?

I have a dataframe like below: A B C 1 1 1 2 0 1 3 0 0 4 1 0 5 0 1 6 0 0 7 1 0 I want the number of occurence of zeroes from df['B'] under the following condition: if(df['B']
hakuna_code
  • 783
  • 7
  • 16
10
votes
2 answers

cumsum with upper & lower limits?

I'd like to find a vectorized way to calculate the cumulative sums of a vector, but with upper and lower limits. In my case, the input only contains 1's and -1's. You can use this assumption in your answer. Of course, a more general solution is also…
Ben.W
  • 199
  • 1
  • 9
9
votes
5 answers

Group numeric vector by predefined maximal group sum

I have a numeric vector like this x <- c(1, 23, 7, 10, 9, 2, 4) and I want to group the elements from left to right with the constrain that each group sum must not exceed 25. Thus, here the first group is c(1, 23), the second is c(7, 10) and…
LulY
  • 976
  • 1
  • 9
  • 24
9
votes
2 answers

Using the pandas Rolling object to create a sliding window of lists

This outstanding post illustrates quite clearly how to use the pandas cumsum() DataFrame method to build a 3D tensor containing a column with lists of lists whose dimensions make them suitable to be used as time series input to an LSTM. I would like…
John Strong
  • 1,844
  • 1
  • 15
  • 24
1
2
3
53 54