Questions tagged [cumulative-sum]

For questions regarding implementations or algorithms for calculating cumulative sums (also known as running totals). Always add the tag for the language/platform!

A cumulative sum (also known as a running total or partial sum) refers to the concept of maintaining only a single value (the sum), which is updated each time a new value is added to the sequence.

1433 questions
3
votes
2 answers

Same month cumulative sum

What is the best way to have a cumulative total of data values when the "month" in the date column is the same? Can this be done using resample() + sum()? I'm thinking I can probably use something off the shelf in python instead of creating a custom…
user1144251
  • 327
  • 1
  • 3
  • 12
3
votes
1 answer

Find number of subarrays with length <= k and with sum == s

I encountered the following question: Given an array of integers arr, a positive integer k, and an integer s, your task is to find the number of non-empty contiguous subarrays with length not greater than k and with a sum equal to s. For arr = [1,…
codeedoc
  • 454
  • 2
  • 7
  • 16
3
votes
2 answers

Cumulative sums in R

I want to compute the following term in R, but without using loops ("for" cycles): So far, I already calculated it manually and tried to use sequence functions for both index variables i and j and integrated them in a cumsum function and summed up…
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
1 answer

How to assign a category to each row based on the cumulative sum of values in spark dataframe?

I have a spark dataframe consist of two columns [Employee and Salary] where salary is in ascending order. Sample Dataframe Expected Output: | Employee |salary | | -------- | ------| | Emp1 | 10 | | Emp2 | 20 | | Emp3 | 30 | |…
itisha
  • 47
  • 5
3
votes
2 answers

Conditional Cumulative Sum Based Multiple Pandas Columns

I have a dataframe that contains multiple "stacks" and their corresponding "lengths". df = pd.DataFrame({'stack-1-material': ['rock', 'paper', 'paper', 'scissors', 'rock'], 'stack-2-material': ['rock', 'paper', 'rock', 'paper', 'scissors'],…
Luxo_Jr
  • 379
  • 1
  • 3
  • 12
3
votes
2 answers

How to calculate Cumulative Average Revenue ? Python

I want to create a graph that will display the cumulative average revenue for each 'Year Onboarded' (first customer transaction) over a period of time. But I am making mistakes when grouping the information I need. Toy Data: dataset = {'ClientId':…
Killi Mandjaro
  • 145
  • 2
  • 15
3
votes
3 answers

Cumulative sum of a pandas column until a maximum value is met, and average adjacent rows

I'm a biology student who is fairly new to python and was hoping someone might be able to help with a problem I have yet to solve With some subsequent code I have created a pandas dataframe that looks like the example below: Distance. No. of…
James
  • 143
  • 1
  • 9
3
votes
3 answers

Assign ID to consecutive groups column r

I would like to produce a column in a data.frame that counts the consecutive id of the groups (s column in dummy df) dummy_df = data.frame(s = c("a", "a", "b","b", "b", "c","c", "a", "a", "c", "c","a","a"), desired_output=…
Vicky Ruiz
  • 113
  • 1
  • 7
3
votes
1 answer

reset cumulative sum based on another column

There are many queries and answers related to conditional cumulative sum (Cumsum Reset based on a condition in Pandas, Reset Cumulative sum base on condition Pandas, Cumsum Reset based on a condition in Pandas). But I am not able to solve the…
Regi Mathew
  • 2,603
  • 3
  • 24
  • 38
3
votes
2 answers

Need Help Generating NULL Entries for Months with No Orders

I have to create output that shows all fields from a table (Tbl) and create an additional column that calculates Cumulative Sum for each customer by month, (eg if a customer has two sales in April, the new column will have the Sum of those sales and…
case11
  • 33
  • 3
3
votes
3 answers

R: Clustered columns running totals in using ggplot2

I have been trying to plot clustered columns with running totals in. I have two types of columns and just need R to calculate the running total of each them separately. For some reason it's adding running totals of different types…
3
votes
2 answers

How to find running sum over two columns in SQL

I have a table like this: month region value 4/1/20 eu-west-2 110 3/1/20 eu-west-2 30 2/1/20 eu-west-2 13 2/1/20 us-west-2 2 1/1/20 us-west-2 242 4/1/20 us-west-2 240 I want to find the cumulative sum for the column 'value' for…
3
votes
1 answer

Pyspark: For each month, make a cumulative sum of the previous 3 months

I'm using PYSPARK and I'm trying to make a cumulative sum of the last 3 months from a specific month: Example: Month Value Jan/19 1 Feb/19 0 Mar/19 4 Apr/19 5 May/19 0 Jun/19 10 So the cumulative sum for each month on the…
thalesthales
  • 95
  • 1
  • 7