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

pandas cumsum that resets when some value is reached

I want to compute a cumsum on some column but reset the sum every time I reach some value. I have read several questions regarding conditional reset on cumsum. They all involve some kind of other column that has the "reset value". I am using the…
4
votes
3 answers

numpy replace 2d bool array with sum of consecutive elements across an axis efficiently

I have a bool array (bool_arr) that I want to replace the consecutive non-zero numbers along the columns with their count (consecutive_count) (which is also the max/last number of the consecutive group) bool_arr = consecutive_count = [[1…
Ta946
  • 1,342
  • 12
  • 19
4
votes
1 answer

Cumulative sum in data table, but using a group-by for each row

Suppose I have a data.table as below (where you can think of w as a grouping variable): set.seed(1) prQ = CJ(Q1 = 1:10, Q2=1:10,w=1:2) prQ[,pQ:=runif(100,0,1)] prQ[,pQ:=pQ/sum(pQ),by=w] > prQ Q1 Q2 w pQ 1: 1 1 1 0.004889560 2:…
wolfsatthedoor
  • 7,163
  • 18
  • 46
  • 90
4
votes
2 answers

cumsum with reset at flagged column in r?

This is my first time asking a question so bear with me. My dataset (df) is like so: animal azimuth south distance pb1 187.561 1 1.992 pb1 147.219 1 8.567 pb1 71.032 0 5.754 pb1 119.502 1 …
NorthLattitude
  • 201
  • 1
  • 12
4
votes
1 answer

add column total to new row in data frame R

Suppose I have the following data. A <- c(4,4,4,4) B <- c(1,2,3,4) C <- c(1,2,4,4) D <- c(3,2,4,1) data <- as.data.frame(rbind(A,B,C,D)) data <- t(data) data <- as.data.frame(data) > data A B C D V1 4 1 1 3 V2 4 2 2 2 V3 4 3 4 4 …
Ellie
  • 415
  • 7
  • 16
4
votes
4 answers

Generating minimum value in cumsum function in R

set.seed(123) dat <- data.frame(day = 1:365, rain = runif(min = 0, max = 5,365),tmean = runif(min = 15, max = 33, 365) ) dat <- dat %>% mutate(mean.daily.rain = mean(rain),mean.daily.tmean = mean(tmean)) %>% mutate(rain.acc = rain -…
89_Simple
  • 3,393
  • 3
  • 39
  • 94
4
votes
4 answers

How to find the maximum consecutive number for multiple columns?

I need to identify the highest number of consecutive values that meet a certain criteria for multiple columns. If my df is: A B C D E 26 24 21 23 24 26 23 22 15 23 24 19 17 11 15 27 22 28 24 24 26 …
4
votes
1 answer

Weird : cumsum not working on dplyr

Context: I want to add cumulative sum column to my tibble named words_uni. I used library(dplyr), function mutate. I work with R version 3.4.1 64 bit - Windows 10 and RStudio Version 1.0.143 > head(words_uni) # A tibble: 6 x 3 # Groups: Type…
Sergio
  • 109
  • 1
  • 9
4
votes
2 answers

Using pandas' groupby with shifting

I am looking to use pd.rolling_mean in a groupby operation. I want to have in each group a rolling mean of the previous elemnets within the same group. Here is an example: id val 0 1 0 2 0 3 1 4 1 5 2 6 Grouping by id,…
splinter
  • 3,727
  • 8
  • 37
  • 82
4
votes
3 answers

Replacing more than n consecutive values in Pandas DataFrame column

Supposing I have the following DataFrame df df = pd.DataFrame({"a" : [1,2,2,2,2,2,2,2,2,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5], "b" : [3,3,3,3,3,3,3,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,7,7], "c" :…
Chris
  • 1,888
  • 4
  • 21
  • 27
4
votes
2 answers

In Python Pandas using cumsum with groupby and reset of cumsum when value is 0

I'm rather new at python. I try to have a cumulative sum for each client to see the consequential months of inactivity (flag: 1 or 0). The cumulative sum of the 1's need therefore to be reset when we have a 0. The reset need to happen as well when…
daphneg
  • 41
  • 1
  • 3
4
votes
2 answers

Counting repeated blocks in pandas

I have the following dataframe and I am trying to label an entire block with a number which is based on how many similar blocks has been seen upto now based on class column. Consecutive class value is given the same number. If the same class block…
learner
  • 2,582
  • 9
  • 43
  • 54
4
votes
5 answers

Reset cumsum as it reaches certain value

I want to reset cumsum over a vector as it reaches certain value. E.g. for the following vector: v <- c(3, 5, 2, 5, 3, 4, 5, 3, 1, 4) expected output is: c(0, 0, 10, 0, 0, 22, 0, 30, 0, 0) With reset <- 10 I can reduce the task to flagging the…
Bulat
  • 6,869
  • 1
  • 29
  • 52
4
votes
2 answers

R cumunique like cumsum

I would like a function that works equivalent to cumsum but rather than adding up it counts the number of unique values so far. I could write a loop for each potential set but that seems like it could get time consuming as my dataset has millions of…
Francis Smart
  • 3,875
  • 6
  • 32
  • 58
4
votes
2 answers

how to rename a column value in pandas dataframe on some condition

I have a pandas dataframe like this: order_id buyer_id phone_no 611 261 9920570003 681 261 9321613595 707 261 9768270700 707 261 9768270700 707 261 9768270700 708 261 …
Neil
  • 7,937
  • 22
  • 87
  • 145