I have a dataframe with multiple columns, however in this case only three are important: Date (datetime), Country and Value. The dataframe contains time-series data which spans over multipe years. For simplicity, assume all numbers in column 'Value' has an initial value of 2.
df
Date Country Value
2020-01-02 UK 2
2020-01-02 USA 2
2020-01-02 AU 2
2020-01-03 UK 2
2020-01-03 USA 2
2020-01-03 AU 2
...
What i want to achieve is to multiply with a factor that changes every month, from a starting value to an ending value. The monthly factor change is calculated as the difference between ending factor and start factor, divided by the number of months.
start_factor = 1.1
end_factor = 1.5
start_date = '2020-02-01'
end_date = '2020-06-01'
I want to multiply the 'Value' column with the correct factor that given month.
Result I want:
Date Country Value
2020-01-01 UK 2
2020-01-01 USA 2
...
2020-02-01 UK 2.2
2020-02-01 USA 2.2
...
2020-03-01 UK 2.4
2020-03-01 USA 2.4
...
2020-04-01 UK 2.6
2020-04-01 USA 2.6
...
2020-05-01 UK 2.8
2020-05-01 USA 2.8
...
2020-06-01 UK 3
2020-06-01 USA 3
...
2020-07-01 UK 3
2020-07-01 USA 3
...
2020-08-01 UK 3
2020-08-01 USA 3
What is the best way of achieving this? I've tried making a date range using pd.date_range(start = start_date, end = end_date, freq='M')
, looping over it and changing factors with df.loc but I'm not sure its the best way of doing it. Also: how would I do it if I just wanted the factors in a new column?