I have a dataset where I have a value for each month that looks like:
Date Platform Name Value
1-31-2019 Pine Dave 100
1-31-2019 Pine Sam 300
1-31-2019 Cherry Sam 200
2-28-2019 Pine Dave 150
2-28-2019 Pine Sam 200
2-28-2019 Cherry Sam 250
I need to use the previous month's value to run calculations, and I'd like to add a column that is last month's value. So I'd like to have an end result that looks like
Date Platform Name Value Previous Value
1-31-2019 Pine Dave 100 NaN
1-31-2019 Pine Sam 300 NaN
1-31-2019 Cherry Sam 200 NaN
2-28-2019 Pine Dave 150 100
2-28-2019 Pine Sam 200 300
2-28-2019 Cherry Sam 250 200
I have the date columns set to a datetime data type. Is it possible to do a date calculation to create a new columns?
df.groupby(['Platform','Name']).Value.shift()would do, given the above comments.