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

Calculate cumsum from the end towards the beginning

I'm trying to calculate the cumsum starting from the last row towards the first for each group. Sample data: t1 <- data.frame(var = "a", val = c(0,0,0,0,1,0,0,0,0,1,0,0,0,0,0)) t2 <- data.frame(var = "b", val = c(0,0,0,0,1,0,0,1,0,0,0,0,0,0,0)) ts…
adl
  • 1,390
  • 16
  • 36
9
votes
4 answers

How to generate sequence considering NaN in pandas

I have a series that contains NaN and True as a value. I want another series to generate a sequence of number, such that whenever NaN comes put that series value as 0 and In between of Two NaN rows I need to perform…
Mohamed Thasin ah
  • 10,754
  • 11
  • 52
  • 111
9
votes
2 answers

Cumulative Summation in intervals - MATLAB

Suppose I have 2 input vectors x and reset of the same size x = [1 2 3 4 5 6] reset = [0 0 0 1 0 0] and an output y which is the cumulative sum of the elements in x. Whenever the value of resets corresponds to 1, the cumulative sum for the…
Alex
  • 93
  • 4
9
votes
4 answers

R, dplyr: cumulative version of n_distinct

I have a dataframe as follows. It is ordered by column time. Input - df = data.frame(time = 1:20, grp = sort(rep(1:5,4)), var1 = rep(c('A','B'),10) ) head(df,10) time grp var1 1 1 1 A 2 2 1 B 3…
steadyfish
  • 847
  • 2
  • 12
  • 27
9
votes
4 answers

Conditional numpy.cumsum?

I'm very new to python and numpy, so sorry if I misuse some terminology. I have converted a raster to a 2D numpy array in the hopes of doing calculations on it quickly and efficiently. I need to get the cumulative sum across a numpy array such…
Vergentorix
  • 95
  • 1
  • 7
9
votes
2 answers

How to get the cumulative sum of numpy array in-place

I want to compute the integral image. for example a=array([(1,2,3),(4,5,6)]) b = a.cumsum(axis=0) This will generate another array b.Can I execute the cumsum in-place. If not . Are there any other methods to do that
Samuel
  • 5,977
  • 14
  • 55
  • 77
8
votes
6 answers

get index of the first block of at least n consecutive False values in boolean array

I have a numpy boolean array w=np.array([True,False,True,True,False,False,False]) I would like to get the index of the first time there are at n_at_least false values. For instance here `n_at_least`=1 -> desired_index=1 `n_at_least`=3 ->…
00__00__00
  • 4,834
  • 9
  • 41
  • 89
8
votes
1 answer

pandas rolling cumsum over the trailing n elements

Using pandas, what is the easiest way to calculate a rolling cumsum over the previous n elements, for instance to calculate trailing three days sales: df = pandas.Series(numpy.random.randint(0,10,10), index=pandas.date_range('2020-01',…
CarlosE
  • 858
  • 2
  • 11
  • 22
8
votes
6 answers

Rolling sums for groups with uneven time gaps

Here's the tweak to my previously posted question. Here's my data: set.seed(3737) DF2 = data.frame(user_id = c(rep(27, 7), rep(11, 7)), date = as.Date(rep(c('2016-01-01', '2016-01-03', '2016-01-05', '2016-01-07', '2016-01-10',…
Kasia Kulma
  • 1,683
  • 1
  • 14
  • 39
8
votes
3 answers

Cumulative sum with lag

I have a very large dataset that looks simplified like this: row. member_id entry_id comment_count timestamp 1 1 a 4 2008-06-09 12:41:00 2 1 b 1 2008-07-14…
Nikolas
  • 132
  • 1
  • 7
7
votes
5 answers

draw random element in numpy

I have an array of element probabilities, let's say [0.1, 0.2, 0.5, 0.2]. The array sums up to 1.0. Using plain Python or numpy, I want to draw elements proportional to their probability: the first element about 10% of the time, second 20%, third…
user1067863
7
votes
1 answer

Difference in outputs using cumsum

Why are these two operations different? library(lubridate) library(magrittr) > seconds_to_period(1:1000) %>% cumsum %>% sum [1] 14492440 > 1:1000 %>% cumsum %>% sum [1] 167167000 I have seen, however, that the issue lies on the fact that cumsum…
Fustincho
  • 423
  • 2
  • 10
7
votes
1 answer

Cumsum within group and reset on condition in pandas

I have a dataframe with two columns ID and Activity. The activity is either 0 or 1. I want a new column containing a increasing number since the last activity was 1. However, the count should only be within one group (ID). If the activity is 1, the…
Siem Peters
  • 193
  • 1
  • 12
7
votes
2 answers

Reset Cumulative sum base on condition Pandas

I have a data frame like: customer spend hurdle A 20 50 A 31 50 A 20 50 B 50 100 B 51 100 B 30 100 I want to calculate additional column for…
user2741956
  • 145
  • 2
  • 6
7
votes
3 answers

Efficient 2d cumsum

Say I have an array like this >>> a = np.arange(1,8).reshape((1,-1)) >>> a array([[1, 2, 3, 4, 5, 6, 7]]) and I want to create, for each of the items in a, a "cumsum of the next 4 items". That is, my expected output is 1, 2, 3, 4, 5, 6,…
FooBar
  • 15,724
  • 19
  • 82
  • 171
1 2
3
53 54