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
7
votes
2 answers

cumsum by group

Suppose data looks like group1 group2 num A sg 1 A sh 2 A sg 4 B at 3 B al 7 a <- cumsum(data[,"num"]) # 1 3 7 10 17 I need something accumulated by groups. In reality,I have multiple columns as grouping…
Lovnlust
  • 1,507
  • 3
  • 29
  • 53
7
votes
1 answer

ggplot2 and cumsum()

I have a set of UNIX timestamps and URIs and I'm trying to plot the cumulative count of requests for each URI. I managed to do that for one URI at a time using a dummy column: x.df$count <- apply(x.df,1,function(row) 1) # Create a dummy column for…
Bob
  • 141
  • 1
  • 8
6
votes
2 answers

Pandas group by cumsum with a flag condition

Assuming i have the following data frame date flag user num 0 2019-01-01 1 a 10 1 2019-01-02 0 a 20 2 2019-01-03 1 b 30 3 2019-03-04 1 b 40 I want to create a cumulative sum of the nums grouped by user only if flag == 1 so i will…
6
votes
2 answers

Counting consecutive 1's in NumPy array

[1, 1, 1, 0, 0, 0, 1, 1, 0, 0] I have a NumPy array consisting of 0's and 1's like above. How can I add all consecutive 1's like below? Any time I encounter a 0, I reset. [1, 2, 3, 0, 0, 0, 1, 2, 0, 0] I can do this using a for loop, but is there…
user308827
  • 21,227
  • 87
  • 254
  • 417
6
votes
1 answer

Python pandas cumsum() reset after hitting max

I have a pandas DataFrame with timedeltas as a cumulative sum of those deltas in a separate column expressed in milliseconds. An example is provided below: Transaction_ID Time TimeDelta CumSum[ms] 1 00:00:04.500 …
wrcobb
  • 543
  • 3
  • 7
  • 17
5
votes
1 answer

adjust the elements of a column to get a cumsum equal to zero

I have this columns in a bigger dataset (here i just report asset "x" but there are different, hence the idea is to replicate the process for every asset): df <- structure(list( asset = c("x", "x", "x", "x", "x", "x", "x", "x", "x", "x",…
5
votes
3 answers

Cumulative sum for more values in one entry

Let's say I have this dataframe (the "number" variable is also from character-type in the original dataframe): df <- data.frame( id = c(1,2,2,1,2), number = c(30.6, "50.2/15.5", "45/58.4", 80, "57/6")) df$number <- as.character(df$number) Now I…
Katharina
  • 139
  • 5
5
votes
2 answers

Calculate cumulative sum from last non-zero entry in python

I have a numeric series like [0,0,0,0,1,1,1,0,0,1,1,0]. I would like to calculate the numeric sum from the last non-zero values. i.e the cumsum will be reset to zero once a zero entry occurs. input:…
AAA
  • 695
  • 1
  • 7
  • 21
5
votes
2 answers

How to use cumsum-Lapply when i+1 column is needed?

I am currently working on a pretty big file containing stops/go of several machinas (about 60) + their production over a long period (more than 60 000 rows). Stops are indexed by "-1" and go by "1" : **Date n1_prod n1_stops …
5
votes
2 answers

Setting pandas global default for skipna to False

For certain Pandas functions, such as sum(), cumsum() and cumprod(), there is an option for skipna which is set to True by default. This causes issues for me as errors might silently propagate so I always explicitly set skipna to False. sum_df =…
Spinor8
  • 1,587
  • 4
  • 21
  • 48
5
votes
2 answers

Rowwise cumulative sum

I have a data.table dt as follows. df <- data.frame(t1 = rep(0,5), t3 = c(12, 5, 8,9, 5), t7= c(25, 48, 7, 9, 14)) dt <- setDT(df) dt t1 t3 t7 1: 0 12 25 2: 0 5 48 3: 0 8 7 4: 0 9 9 5: 0 5 14 I want to get the cumulative sums across…
Crops
  • 5,024
  • 5
  • 38
  • 65
5
votes
3 answers

How do I calculate the number of consecutive columns with zero values from the right until the first non zero element occurs

Suppose I have the following dataframe: C1 C2 C3 C4 0 1 2 3 0 1 4 0 0 0 2 0 0 0 3 3 0 3 0 0 Then I want to add another column such that it will display the number of zero valued column that occur contiguously from the…
Alex_ban
  • 221
  • 2
  • 11
5
votes
5 answers

resetting cumsum if value goes to negative in r

ve <- c(17, -9, 9, -17, 17, -17, 11, -9, 16, -18, 17, 0, 0, -18, 17, 0, 0, -17, 14, -14, 17, -2, 0, -15, 9, -9, 17, -16, 16, -17, 17, -17, 17, -17, 17, -17, 17, -8, 7, -16, 17, -14, 14, -10, 10, -16, 16, -10, 10, -12, 12, -11, 11, -17, 17, -17, 17,…
user5813583
  • 133
  • 10
5
votes
2 answers

Streaks of True or False in pandas Series

I'm trying to work out how to show streaks of True or False in a pandas Series. Data: p = pd.Series([True,False,True,True,True,True,False,False,True]) 0 True 1 False 2 True 3 True 4 True 5 True 6 False 7 False 8 …
nipy
  • 5,138
  • 5
  • 31
  • 72
5
votes
2 answers

Conditional mutate cumsum dlpyr

I have towns (from A to D), which have different populations, and are at different distances. The objective is to add up the total population living within the circle of radius (distance XY) where X is a town in the centre of the circle and Y any…
JPV
  • 323
  • 2
  • 10