I have a dataframe like this
ID, DateIndex, Qty
1, 1, 10
2, 1, 15
3, 1, 20
4, 1, 30
1, 2, 14
2, 2, 13
3, 2, 14
4, 2, 12
1, 3, 1
2, 3, 60
3, 3, 19
4, 3, 12
....
I want to output a table like this
ID, DateIndex, Qty, n-1, n-2, n-3, n-4....
1, 3, 1, -1, -1, 0, 0....
2, 3, 60, 1, 1, 0, 0....
3, 3, 19, 1, -1, 0, 0....
4, 3, 12, 0, -1, 0, 0....
The conditional is that if the qty value of that dateindex is less than the qty value for that ID at dateindex-1 it will return -1, if it is greater than it will return 1, and if it is the same or not found then it will return 0.
Here is what I have so far
import pandas
import numpy as np
df = pandas.read_csv('test.csv', parse_dates=['Date']).sort_values(['Date', 'ID'])
df['DateIndex'] = df['Date'].rank(method='dense')
I think I will need to define a function and use apply but not sure how to do it