cumsum

Assign id to item over groups with certain rules

Assign id to item over groups with certain rules Question: I have a dataframe looks like this df = pd.DataFrame({‘type’: [‘A’, ‘A’, ‘A’, ‘B’, ‘B’, ‘B’, ‘A’, ‘A’,’C’,’D’,’C’,’D’,’D’,’A’, ‘A’], }) I wanna create a unique id based on the group of the type column, but it will still cumsum when the type equals to ‘A’ …

Total answers: 1

Problem with Cumsum Single Column Groupby

Problem with Cumsum Single Column Groupby Question: my mind is blown so hoping you can help here:) I have data that I need to ‘cumsum’ by month and year and cannot figure out how to do it. My data looks like this – testdf: month year power_kwh 0 1 1990 416491241.4 1 2 1990 343033068.2 …

Total answers: 1

Cumulative Sum based on a Trigger

Cumulative Sum based on a Trigger Question: I am trying to track cumulative sums of the ‘Value’ column that should begin every time I get 1 in the ‘Signal’ column. So in the table below I need to obtain 3 cumulative sums starting at values 3, 6, and 9 of the index, and each sum …

Total answers: 1

pandas cumsum on lag-differenced dataframe

pandas cumsum on lag-differenced dataframe Question: Say I have a pd.DataFrame() that I differenced with .diff(5), which works like "new number at idx i = (number at idx i) – (number at idx i-5)" import pandas as pd import random example_df = pd.DataFrame(data=random.sample(range(1, 100), 20), columns=["number"]) df_diff = example_df.diff(5) Now I want to undo this …

Total answers: 1

pandas restart cumsum every time the value is zero

pandas restart cumsum every time the value is zero Question: so I have a series, I want to cumsum, but start over every time I hit a 0, somthing like this: orig wanted result 0 0 0 1 1 1 2 1 2 3 1 3 4 1 4 5 1 5 6 1 6 …

Total answers: 2

Applying an equation by group in my python/pandas

Applying an equation by group in my python/pandas Question: I am trying to create an equation that does Revenue – Cost and them cumulates the sums by group. Is there a way to use group by with an equation? I have provided some sample data below. Data: stack = pd.DataFrame({‘ID’: [1, 2, 3, 4, 5, …

Total answers: 2

Cumulatively count values between range by group in a pandas dataframe

Cumulatively count values between range by group in a pandas dataframe Question: Say I have the following data. For each user_id I want to get a cumulative count every time the difference score is <= -2 until it reaches a positive value. The count should then reset to zero and stay at that value until …

Total answers: 1

Numpy array counter with reset

Numpy array counter with reset Question: I have a numpy array with only -1, 1 and 0, like this: np.array([1,1,-1,-1,0,-1,1]) I would like a new array that counts the -1 encountered. The counter must reset when a 0 appears and remain the same when it’s a 1: Desired output: np.array([0,0,1,2,0,1,1]) The solution must be very …

Total answers: 5

Python – How to do accumulative sums depending on the value of a column

Python – How to do accumulative sums depending on the value of a column Question: I have a dataframe and I want to add a column that should be the accumulative sum of one of the columns but only if the value of another column is a specific one. For example, my dataframe is as …

Total answers: 2

Is there a way to cumsum value based on the flag and add the value to other flag consecutive rows without iteration in pandas

Is there a way to cumsum value based on the flag and add the value to other flag consecutive rows without iteration in pandas Question: I’m trying to cumsum ‘value’ for ‘flag’==2, and add those cummulative values to consecutive rows of ‘flag’==1 and dropped flag 2 rows. Input Index_Date flag value ======================== 2020-01-31 1 10 …

Total answers: 2