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

Index of the element in the series where the cumulative some is equal to or greater than 10

I have this Panda series of integers, for example, s = pd.Series([1, 2, 3, 1, 5, 10]). I need to find the index of the element starting from which the cumulative sum is equal to or greater some number, for example, 10. So, for this series since…
alekscooper
  • 795
  • 1
  • 7
  • 19
-1
votes
1 answer

Python Pandas running total column

I have a df I want to add a running total column to date F1 F2 2020-12-1 10 5 2020-12-3 9 6 2020-12-4 7 16 Expected output: date F1 F2 F1 Running Total F2 Running…
-1
votes
1 answer

length of np.cumsum(input) is longer than input array -- how is this possible?

Running the below code in Colab. Have two separate instances running (separate files). In one instance the code works, in the other, it does not. In the case where it's not working, the funciton np.cumsum() appears to be returning an array twice…
-1
votes
2 answers

R Problem Using cumsum in data.table to Forward Fill

library(data.table) set.seed(123) dt = data.table( grp=round(runif(10)), val=c(runif(4), NA, runif(4), NA) ) dt Output is: grp val 1: 0 0.95683335 2: 1 0.45333416 3: 0 0.67757064 4: 1 0.57263340 5: 1 NA 6: 0…
TMo
  • 435
  • 4
  • 11
-1
votes
1 answer

Problem using cumsum() along mutate function

I have this simplified df that I'm tryng to get the cumsum() for each group library(dplyr) Country <- c(rep("A",5), rep("B",4), rep("C", 5)) Date <- 1:14 Value <- c(2:6, 10:13, 7:11) df <- data.frame(Date, Country, Value) > view(df) Date…
Jones
  • 333
  • 1
  • 11
-1
votes
1 answer

Create a function and use a loop to calculate the cumulative date and id

I have an coding exercise: create a "function", then use "for" to calculate the cumulative by id and date. I can only use cumsum() and for, all packages are not allowed. For instance, I create a data frame…
MMC
  • 11
  • 1
-1
votes
2 answers

How to find the row index of Pandas dataframe at which a column CumSum equals X?

Consider the following DataFrame df: index A 0 -1 1 0 2 1 3 -1 # <==== here, df['A'].cumsum() == 0, starting from last row 4 -1 5 -1 6 1 7 -1 8 1 9 1 10 1 I am trying to…
pepe
  • 9,799
  • 25
  • 110
  • 188
-1
votes
4 answers

Repeat dataframe rows based on cumsum index

I have a dataframe as follows: data.frame(title="Title", bk=c("Book 1", "Book 1", "Book 3"), ch=c("Chapter 1", "Chapter 2", "Chapter 1")) title bk ch 1 Title Book 1 Chapter 1 2 Title Book 1 Chapter 2 3 Title Book 3 Chapter 1 How do I…
Sati
  • 716
  • 6
  • 27
-1
votes
3 answers

iterative cumsum where sum determines the next position to be added

I have a data.table as follows set.seed(5) x <- data.table(x=sample(1:20,15)) > x x 1: 5 2: 14 3: 17 4: 20 5: 2 6: 11 7: 8 8: 15 9: 12 10: 16 11: 3 12: 18 13: 10 14: 4 15: 13 and I would like to start at 1 and cumulate values…
bumblebee
  • 1,116
  • 8
  • 20
-1
votes
1 answer

Pandas: cumsum by group across time, resampled to entire dataframe

I have a time series data frame. I'm interested in plotting the cumulative sums across time, by groups of [column1, column2]. So far I can only plot the cumulative sum at the points where the group combination existed. What I want is the cumulative…
user3055163
  • 471
  • 4
  • 10
-1
votes
2 answers

How do I get a cumulative sum using cumsum in MATLAB?

This is code for i = 1 : 5 b = i; a=cumsum(b); end fprintf('%f \n', a); I expected 1 + 2 + 3 + 4 + 5 = 15 so I would print 15 at the end. But it output 5.000000. If i code "a = cumsum (b)" outside the for loop, it will not be…
ONION
  • 239
  • 1
  • 4
  • 13
-1
votes
1 answer

Raster cumulative sums with Zapply in R

I have a multi year daily raster time series stack (ras_in) and I need to calculate cumulative sums (e.g. day1 = day1; day2 = day1 + day2; day3 = day1 + day2 + day3,...) by year I am trying to use 'cumsum' function and Zapply as follow: fun_my <-…
Gianca
  • 109
  • 10
-1
votes
1 answer

R, Cumsum function on non-concatinative variable realisations

I have a 306x19 data-set and I would like to use the Cumsum function on say, VarF only when VarA takes a certain value. On a high-level description, the idea is to filter on VarA and apply a cumsum on the remaining of the dataset (VarF). I would…
SolitonK
  • 103
  • 2
-1
votes
1 answer

Cumulative sums of matrix columns with calculation

I have a matrix that looks like this: date 1 2 3 4 201601 2 4 6 1 201602 3 7 7 4 201603 4 8 9 6 201604 6 4 5 7 Rows indicates prod_Date and column…
Elly
  • 129
  • 2
  • 12
-1
votes
1 answer

MATLAB APPLY CUMSUM IN STEPS

I have data of integers in x = 500 X 612 matrix. I need a new variable xx in a 500 X 612 matrix but I need to apply cumsum along each row (500) across 12 column steps and applying cumsum like this 51 times --> 500 X (12 X 51) matrix. Then I need a…
user2100039
  • 1,280
  • 2
  • 16
  • 31