rolling-sum

how to do forward rolling sum in pandas?

倖福魔咒の 提交于 2020-01-13 08:57:08
问题 I have this dataframe: dates = pd.date_range(start='2016-01-01', periods=20, freq='d') df = pd.DataFrame({'A': [1] * 20 + [2] * 12 + [3] * 8, 'B': np.concatenate((dates, dates)), 'C': np.arange(40)}) I sorted the data frame by Date: df.sort_values('B',inplace=True) I am looking to do a forward rolling sum on date. However, I can only do backward rolling sum using: df.groupby('A').rolling(7, on='B',min_periods=0).C.sum() A B 1 2016-01-01 0.0 2016-01-02 1.0 2016-01-03 3.0 2016-01-04 6.0 2016-01

Conditional rolling sum of events with 'ragged' dates

非 Y 不嫁゛ 提交于 2020-01-04 01:21:08
问题 Introduction I am using R to analyze the 'momentum' of protest movements in Africa. To do so, I am analyzing individual protest events. I want to create a rolling measure of the rolling number (sum) of protests within a time period . Most of the answers here on Stack Overflow deal with datasets where observations are at fixed intervals (one obs. per day or per month, etc.). But my data are 'ragged' in the sense that they occur in different intervals. Sometimes there is one day between

Calculating sum of array elements and reiterate for entire array in MATLAB

别来无恙 提交于 2019-12-24 22:12:19
问题 I have a vector A of size 7812x1 and would like to calculate the sum of fixed windows of length 21 (so 372 blocks). This should be reiterated, so that the output should return a vector of size 372x1. I have t=7812 , p=372 , w=21 ; for t=1:p out = sum(A((t*w-w+1):(t*w))); end This code, however, does not work. My idea is that the part ((t*w-w+1):(t*w)) allows for something like a rolling window. The window is of length 21, so there is not really a need to express is with variables, yet I think

Rolling sum in dplyr

。_饼干妹妹 提交于 2019-12-07 18:04:36
问题 set.seed(123) df <- data.frame(x = sample(1:10, 20, replace = T), id = rep(1:2, each = 10)) For each id , I want to create a column which has the sum of previous 5 x values. df %>% group_by(id) %>% mutate(roll.sum = c(x[1:4], zoo::rollapply(x, 5, sum))) # Groups: id [2] x id roll.sum <int> <int> <int> 3 1 3 8 1 8 5 1 5 9 1 9 10 1 10 1 1 36 6 1 39 9 1 40 6 1 41 5 1 37 10 2 10 5 2 5 7 2 7 6 2 6 2 2 2 9 2 39 3 2 32 1 2 28 4 2 25 10 2 29 The 6th row should be 35 (3 + 8 + 5 + 9 + 10) , the 7th row

Rolling sum in dplyr

时光毁灭记忆、已成空白 提交于 2019-12-05 18:45:54
set.seed(123) df <- data.frame(x = sample(1:10, 20, replace = T), id = rep(1:2, each = 10)) For each id , I want to create a column which has the sum of previous 5 x values. df %>% group_by(id) %>% mutate(roll.sum = c(x[1:4], zoo::rollapply(x, 5, sum))) # Groups: id [2] x id roll.sum <int> <int> <int> 3 1 3 8 1 8 5 1 5 9 1 9 10 1 10 1 1 36 6 1 39 9 1 40 6 1 41 5 1 37 10 2 10 5 2 5 7 2 7 6 2 6 2 2 2 9 2 39 3 2 32 1 2 28 4 2 25 10 2 29 The 6th row should be 35 (3 + 8 + 5 + 9 + 10) , the 7th row should be 33 (8 + 5 + 9 + 10 + 1) and so on. However, the above function is also including the row

how to do forward rolling sum in pandas?

不问归期 提交于 2019-12-05 04:32:09
I have this dataframe: dates = pd.date_range(start='2016-01-01', periods=20, freq='d') df = pd.DataFrame({'A': [1] * 20 + [2] * 12 + [3] * 8, 'B': np.concatenate((dates, dates)), 'C': np.arange(40)}) I sorted the data frame by Date: df.sort_values('B',inplace=True) I am looking to do a forward rolling sum on date. However, I can only do backward rolling sum using: df.groupby('A').rolling(7, on='B',min_periods=0).C.sum() A B 1 2016-01-01 0.0 2016-01-02 1.0 2016-01-03 3.0 2016-01-04 6.0 2016-01-05 10.0 2016-01-06 15.0 I want to do forward rolling sum. I believe need change ordering by iloc[::-1]

Pandas rolling sum on string column

余生长醉 提交于 2019-12-02 04:27:20
问题 I'm using Python3 with pandas version '0.19.2'. I have a pandas df as follows: chat_id line 1 'Hi.' 1 'Hi, how are you?.' 1 'I'm well, thanks.' 2 'Is it going to rain?.' 2 'No, I don't think so.' I want to group by 'chat_id', then do something like a rolling sum on 'line' to get the following: chat_id line conversation 1 'Hi.' 'Hi.' 1 'Hi, how are you?.' 'Hi. Hi, how are you?.' 1 'I'm well, thanks.' 'Hi. Hi, how are you?. I'm well, thanks.' 2 'Is it going to rain?.' 'Is it going to rain?.' 2

Pandas rolling sum on string column

房东的猫 提交于 2019-12-02 01:25:26
I'm using Python3 with pandas version '0.19.2'. I have a pandas df as follows: chat_id line 1 'Hi.' 1 'Hi, how are you?.' 1 'I'm well, thanks.' 2 'Is it going to rain?.' 2 'No, I don't think so.' I want to group by 'chat_id', then do something like a rolling sum on 'line' to get the following: chat_id line conversation 1 'Hi.' 'Hi.' 1 'Hi, how are you?.' 'Hi. Hi, how are you?.' 1 'I'm well, thanks.' 'Hi. Hi, how are you?. I'm well, thanks.' 2 'Is it going to rain?.' 'Is it going to rain?.' 2 'No, I don't think so.' 'Is it going to rain?. No, I don't think so.' I believe df.groupby('chat_id')[