I have a dataframe that consists of zipcodes, with a quarter and a cumulative number. This number contains all the events that happened in that zipcode up and including that quarter. I want to add a column, e.g., 'new', that notes the change in events in that quarter. My current dataframe looks like this:
Zipcode Quarter Cumulative_number
9999 2020 Q1 1
9999 2020 Q2 1
9999 2020 Q3 2
9999 2020 Q4 3
2000 2018 Q2 0
2000 2018 Q3 4
2000 2018 Q4 0
Ideally the dataframe would look like this:
Zipcode Quarter Cumulative_number New
9999 2020 Q1 1 0
9999 2020 Q2 1 0
9999 2020 Q3 2 1
9999 2020 Q4 3 1
2000 2018 Q2 0 0
2000 2018 Q3 4 4
2000 2018 Q4 4 0
The code used to create the cumulative number looks like this:
df = (df.reindex(pd.date_range(df.columns.min(), until, freq='QS', name='Quarter'), axis=1)
.ffill(axis=1)
.stack()
.astype(int)
.reset_index(name='Cumulative_number'))