I have this DataFrame (this is just an example, not real data):
In [1]: import pandas as pd
my_data = [{'client_id' : '001', 'items' : '10', 'month' : 'Jan'},
{'client_id' : '001', 'items' : '20', 'month' : 'Feb'},
{'client_id' : '001', 'items' : '30', 'month' : 'Mar'},
{'client_id' : '002', 'items' : '30', 'month' : 'Jan'},
{'client_id' : '002', 'items' : '20', 'month' : 'Feb'},
{'client_id' : '002', 'items' : '15', 'month' : 'Mar'},
{'client_id' : '003', 'items' : '10', 'month' : 'Jan'},
{'client_id' : '003', 'items' : '20', 'month' : 'Feb'},
{'client_id' : '003', 'items' : '15', 'month' : 'Mar'}]
df = pd.DataFrame(my_data)
In [2]: df
Out [2]:
client_id month items
0 001 Jan 10
1 001 Feb 20
2 001 Mar 30
3 002 Jan 30
4 002 Feb 20
5 002 Mar 15
6 003 Jan 10
7 003 Feb 20
8 003 Mar 15
I want to calculate the delta items purchased for every couple of months. That is, for example, the customer "001" bought 10 more items in February (20) than in January (10). Customer "002", bought -10 pieces (February 20, January 30). The final DataFrame will look like this:
In [3]: delta_df
Out [3]:
client_id delta_items_feb delta_items_mar
0 001 10 10
1 002 -10 -5
2 003 10 -5
Any thoughts on how to do this?