Search code examples
pythonpandasdataframecumsum

Update values in pandas df column by adding fixed variable to preceding value


I am trying to project out the value in one column by adding a fixed value. Essentially, it would be like a sheet in Excel where there would be a value in cell a1, a fixed value in cell b1, and a2 would be =sum(a1+$b$1) and dragging the cells down. It seems like that would be straightforward in pandas, perhaps involving .cumsum, but I haven't found a way to get my desired result.

Here is my data frame.

df = pd.DataFrame({"date_column" : ["2020-08-16", "2020-08-17", "2020-08-18", "2020-08-19", "2020-08-20" , "2020-08-21", "2020-08-22", "2020-08-23", "2020-08-24", "2020-08-25" ] , 
"purchases" : [23,'','','','','','','','','']})

df
date_column    purchases
0   2020-08-16     23
1   2020-08-17    
2   2020-08-18    
3   2020-08-19    
4   2020-08-20    
5   2020-08-21    
6   2020-08-22    
7   2020-08-23    
8   2020-08-24    
9   2020-08-25  

Separately, I have calculated a variable called avg_increase for the preceding two weeks.

avg_increase
2.5000000

I want to add the avg_increase value to the initial value in the purchases column, and then add the avg_increase to that value, and so on. The desired output is:

date_column    purchases
0   2020-08-16     23
1   2020-08-17    25.5
2   2020-08-18    28
3   2020-08-19    30.5
4   2020-08-20    33
5   2020-08-21    35.5
6   2020-08-22    38
7   2020-08-23    40.5
8   2020-08-24    43
9   2020-08-25    45.5

Solution

  • Alternative solution assuming ordered index 0..N:

    df.purchases = df.loc[0,"purchases"]+df.index.values*2.5
    

    result:

      date_column  purchases
    0  2020-08-16       23.0
    1  2020-08-17       25.5
    2  2020-08-18       28.0
    3  2020-08-19       30.5
    4  2020-08-20       33.0
    5  2020-08-21       35.5
    6  2020-08-22       38.0
    7  2020-08-23       40.5
    8  2020-08-24       43.0
    9  2020-08-25       45.5
    

    Simply get first value of purchases and add index values multiplied by 2.5