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
1 answer

"Cumulative difference" function in R

Is there a pre-existing function to calculate the cumulative difference between consequtive values? Context: this is to estimate the change in altitude that a person has to undergo in both directions on a journey generated by…
RobinLovelace
  • 4,799
  • 6
  • 29
  • 40
4
votes
2 answers

Reversing cumulative sum to determine actuals in R

I have a df where each row is the cumulative sum of the row above it. Is there a way to derive the original values from this df? X1 X2 1 1 5 2 3 9 3 6 12 4 10 14 5 15 15 Desired output: X1 X2 1 1 5 2 2 4 3 3 3 4 4 2 5 5 …
Chris S
  • 55
  • 3
3
votes
2 answers

Fill gaps between 1's in Pandas dataframe column with increment values that reset when next 1 is reached

Apparently this is a more complicated problem than I thought. All I want to do is fill the zeros with ++1 increments until the next 1 My dataset is 1m+ rows, so I'm trying to vectorize this operation if possible. Here's a sample column: # Define the…
nborel
  • 33
  • 4
3
votes
2 answers

Keep row as soon as cumulative value reaches a certain threshold R

I have a dataframe where I would like to keep a row as soon as the cumulative value of a column reaches a certain level. The dataset could look like this: set.seed(0) n <- 10 dat <- data.frame(id=1:n, …
reinoud
  • 63
  • 6
3
votes
1 answer

Binary search tree cumsum

Problem: Given a binary search tree in which the keys are numbers, we'll define the operation 'cumsum' ( shorthand for cumulative sum ) that switches the key of every node in the tree with the sum of all the keys that are smaller or equal to it. For…
jacob12
  • 173
  • 8
3
votes
1 answer

Cumsum column while skipping rows or setting fixed values on a condition based on the result of the actual cumsum

I'm trying to find a vectorized solution in pandas that is quite common in spreadsheets which is to cumsum while skipping or setting fixed values on a condition based on the result of the actual cumsum. I have the following: A 1 0 2 -1 3 …
Gus
  • 193
  • 9
3
votes
1 answer

Matlab find() function in R

I am trying to convert some Matlab code into R. The code looks something like this: u= [.4 .5 .1; .2 .4 .4; .4 .1 .5] z= find(cumsum(u)>= rand,1) OUTPUT: e.g. z = 3 The mathematical background is described like this (from a math tut.…
3
votes
3 answers

Cumulative sum in pandas starting with a zero and ending with the sum of all but the last entry respecting groups

In the dataframe below, I want to create a new column C which will be the cumulative sum of B for each group in the A column, but these sums must start at zero and only add the values until the penultimate entry for that group. A B 0 1 5 1 1 6 2 2…
William M.
  • 235
  • 1
  • 9
3
votes
1 answer

Pandas cumulative sum depending on other columns value

I have a Dataset like this Date Runner Group distance [km] 2021-01-01 Joe 1 7 2021-01-02 Jack 1 6 2021-01-03 Jess 1 9 2021-01-01 Paul 2 11 2021-01-02 …
Peter
  • 81
  • 1
  • 1
  • 5
3
votes
5 answers

Groupby of different columns with different aggreagation with cumsum with another dataset

I have a dataframe sorted by date and time as : df1: ID Date A_sum A_count B_sum B_count A_last B_last abc 01/jan 26 2 25 2 0 0 xyz 01/jan 54 3 45 3 4 6 df2: ID …
naina
  • 303
  • 3
  • 14
3
votes
1 answer

Calculating sum of up to the current row in pandas while iterating on each row in a time series data

Suppose I have the following code that calculates how many products I can purchase given my budget- import math import pandas as pd data = [['2021-01-02', 5.5], ['2021-02-02', 10.5], ['2021-03-02', 15.0], ['2021-04-02', 20.0]] df =…
user1330974
  • 2,500
  • 5
  • 32
  • 60
3
votes
3 answers

Acumulative sum in R

I have data containing columns x and lx, I want to get cumulative sum on lx basis. My data is like: x l(x) 20 100.000 21 99.644 22 99.286 23 98.925 24 98.561 25 98.195 26 97.829 and I want to get output like…
3
votes
1 answer

groupby cumsum sorted dataframe

I want to group a dataframe by a column then apply a cumsum over the other ordered by the first column descending df1: id PRICE DEMAND 0 120 10 1 232 2 2 120 3 3 232 8 4 323 5 5 323 6 6 …
3
votes
1 answer

Cumulative Sum of Grouped Strings in Pandas

I have a pandas data frame that I want to group by two columns and then return the cumulative sum of a third column of strings as a list within one of these groups. Example: Year Bucket Name 2000 1 A 2001 1 B 2003 1 C 2000 …
JBN
  • 67
  • 4
3
votes
2 answers

Rolling cumsum in data.table

Trying to get (reverse) cumulative sums in a moving window by group in data.table. For example, from the following data I'd like to get those values in the "roll_cumsum" column: dt = data.table() dt[, a := seq(1, 10, 1)] dt[, group := rep(1:2, each…
jayc
  • 329
  • 1
  • 8