I have a dataframe df
ID KD DT
0 1 2 5.6
1 1 5 8.7
4 4 9 1.9
5 4 2 1.7
6 4 7 8.8
2 6 9 8.3
3 6 7 7.2
9 7 36 3.1
10 7 2 2.2
12 7 7 5.6
I want to create a dataframe such that for each unique KD value, new columns of {-1,0,1} are added depending on ID (from a list of ID values) and DT. ID = [1,2,4,6,7,8]. New dataframe should have len(ID)+1 columns with first column the unique KD value and len(D) columns such that column ID = 1 if df.loc[(df.ID==id) & (df.KD==kd),'DT'] >= 5, column ID = 0 if (kd,id) pair is not in df and column ID = -1 if df.loc[(df.ID==id) & (df.KD==kd),'DT'] < 5
For the dataframe given above new dataframe should be
df2
KD 1 2 4 6 7 8
0 2 1 0 -1 0 -1 0
1 5 1 0 0 0 0 0
2 7 0 0 1 1 1 0
3 9 0 0 -1 1 0 0
4 36 0 0 0 0 -1 0
In fact, number of unique KD and ID are very large (in the range of 10K). Any help in finding a very efficient way to do this. please ?