cumsum

Filling gaps for cumulative sum with Pandas

*爱你&永不变心* 提交于 2020-01-04 06:19:19
问题 I'm trying to calculate the inventory of stocks from a table in monthly buckets in Pandas. This is the table: Goods | Incoming | Date -------+------------+----------- 'a' | 10 | 2014-01-10 'a' | 20 | 2014-02-01 'b' | 30 | 2014-01-02 'b' | 40 | 2014-05-13 'a' | 20 | 2014-06-30 'c' | 10 | 2014-02-10 'c' | 50 | 2014-05-10 'b' | 70 | 2014-03-10 'a' | 10 | 2014-02-10 This is my code so far: import pandas as pd df = pd.DataFrame({ 'goods': ['a', 'a', 'b', 'b', 'a', 'c', 'c', 'b', 'a'], 'incoming':

Calculating cumulative returns with pandas dataframe

会有一股神秘感。 提交于 2020-01-01 02:30:08
问题 I have this dataframe Poloniex_DOGE_BTC Poloniex_XMR_BTC Daily_rets perc_ret 172 0.006085 -0.000839 0.003309 0 173 0.006229 0.002111 0.005135 0 174 0.000000 -0.001651 0.004203 0 175 0.000000 0.007743 0.005313 0 176 0.000000 -0.001013 -0.003466 0 177 0.000000 -0.000550 0.000772 0 178 0.000000 -0.009864 0.001764 0 I'm trying to make a running total of daily_rets in perc_ret however my code just copies the values from daily_rets df['perc_ret'] = ( df['Daily_rets'] + df['perc_ret'].shift(1) )

R data.table cumulative sum function

孤街醉人 提交于 2019-12-25 12:42:43
问题 I have created the following reproducible example: library(data.table) Col_1 <- 0.05 Col_2 <- c( "B", "A", "C", "B", "C", "A", "C", "B", "B", "A" ) Col_3 <- 1000 Col_4 <- "" data <- data.frame( Col_1, Col_2, Col_3, Col_4 ) mydata.table <- as.data.table( data )[ , list( Col_1, Col_2, Col_3, Col_4 ) ] Col1 <- "Col_1"; Col2 <- "Col_2"; Col3 <- "Col_3"; Col4 <- "Col_4" mydata.table[, (Col4) := ifelse( get( Col2 ) == "A" , get( Col1 ) * get( Col3 ), "0" ) ] mydata.table[ , (Col3) := cumsum( c(

R data.table cumulative sum function

可紊 提交于 2019-12-25 12:42:04
问题 I have created the following reproducible example: library(data.table) Col_1 <- 0.05 Col_2 <- c( "B", "A", "C", "B", "C", "A", "C", "B", "B", "A" ) Col_3 <- 1000 Col_4 <- "" data <- data.frame( Col_1, Col_2, Col_3, Col_4 ) mydata.table <- as.data.table( data )[ , list( Col_1, Col_2, Col_3, Col_4 ) ] Col1 <- "Col_1"; Col2 <- "Col_2"; Col3 <- "Col_3"; Col4 <- "Col_4" mydata.table[, (Col4) := ifelse( get( Col2 ) == "A" , get( Col1 ) * get( Col3 ), "0" ) ] mydata.table[ , (Col3) := cumsum( c(

Multiple Conditional Cumulative Sum in R

喜你入骨 提交于 2019-12-24 20:11:54
问题 This is my data frame as given below rd <- data.frame( Customer = rep("A",15), date_num = c(3,3,9,11,14,14,15,16,17,20,21,27,28,29,31), exp_cumsum_col = c(1,1,2,3,4,4,4,4,4,5,5,6,6,6,7)) I am trying to get column 3 ( exp_cumsum_col ), but am unable to get the correct values after trying many times. This is the code I used: rd<-as.data.frame(rd %>% group_by(customer) %>% mutate(exp_cumsum_col = cumsum(row_number(ifelse(date_num[i]==date_num[i+1],1))))) If my date_num is continuous, then I am

Cumsum excluding current value

蹲街弑〆低调 提交于 2019-12-24 20:03:00
问题 I am new to R and I am trying to write a function to cumulatively sum previously ordered items by customers. I have already found an almost-fitting example of code on Stack Overflow, but I do not manage to modify it accordingly to my needs. This is the code: Fruits <- Fruits[order(Cars$order.id), ] #sort data Fruits$prev_Apples<-with(Fruits, ave( ave(Apples, customer.id, FUN=cumsum), #get running sum per customer.id interaction(customer.id, order.id, drop=T), FUN=max, na.rm=T) #find largest

r cumulative sum function, conditions

瘦欲@ 提交于 2019-12-24 14:45:46
问题 I have a data frame in R , fairly large 600 rows/observations one column is patientId NOT in numeric form,e.g ju89, ju87, so it's a factor column one column is remission 1/0 where 1 means remission 0 means not remission one column is timefromdiagnosis now, from time from diagnosis patients go from 1 to 0, 0 to 0, 0 to 1 or 1 to 1 I want to add a column to the data frame where it is 1 when a patient has 0 in remission 2 when precisely a patient has 1 in remission and the last time he had 0 OR

python: vectorized cumulative counting

半世苍凉 提交于 2019-12-24 09:58:21
问题 I have a numpy array and would like to count the number of occurences for each value, however, in a cumulative way in = [0, 1, 0, 1, 2, 3, 0, 0, 2, 1, 1, 3, 3, 0, ...] out = [0, 0, 1, 1, 0, 0, 2, 3, 1, 2, 3, 1, 2, 4, ...] I'm wondering if it is best to create a (sparse) matrix with ones at col = i and row = in[i] 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0 Then we could

Conditional count of cumulative sum Dataframe - Loop through columns

≯℡__Kan透↙ 提交于 2019-12-24 01:27:31
问题 Im trying to compute a cumulative sum with a reset within a dataframe, based on the sign of each values. The idea is to the same exercise for each column separately. For example, let's assume I have the following dataframe: df = pd.DataFrame({'A': [1,1,1,-1,-1,1,1,1,1,-1,-1,-1],'B':[1,1,-1,-1,-1,1,1,1,-1,-1,-1,1]},index=[0, 1, 2, 3,4,5,6,7,8,9,10,11]) For each column, I want to compute the cumulative sum until I find a change in sign; in which case, the sum should be reset to 1. For the

R: Cumulatively count number of times column value appears in other column

早过忘川 提交于 2019-12-22 18:18:04
问题 It is probably easier to describe what I want to do using an example... Say I have the following dataframe: id1 id2 var 1 2 a 2 3 b 2 1 a 3 2 a 2 3 a 4 2 a 3 1 b Which you can generate as follows df <- data.frame(id1 = c(1,2,2,3,2,4,3), id2 = c(2,3,1,2,3,2,1), var = c('a','b','a','a','a','a','b')) I want a cumulative count of the number of times id2 has appeared in id1 with the same var, so I would end up with id1 id2 var count 1 2 a 0 2 3 b 0 2 1 a 1 3 2 a 1 2 3 a 1 4 2 a 2 3 1 b 0 So the