I have a DataFrame grouped by a categorical feature. For example, I have df
df[['APP_NO', 'REPAY_METHOD', 'RESIDUAL_DEBT']] \
.groupby(['APP_NO', 'REPAY_METHOD']).agg({'RESIDUAL_DEBT' : 'sum'})
ID NUM CAT_FEAT aggr
1 123 2 1233
2 234 2 6631
3 576 -1 -491
4 987 0 5461
NUM is an unique identifier
As a result, I want to get the following daraframe:
ID NUM CAT_FEAT aggr_CF2 aggr_CF0 aggr_CFm1
1 123 2 1233 -1 -1
2 234 2 6631 -1 -1
3 576 -1 -1 -1 -491
4 987 0 -1 5461 -1
That is, for each NUM, get the aggr tag with all CAT_FEAT values
If NUM does not have any value from CAT_FEAT, then replace it with -1
The question is how to implement this most correctly. The current df is already grouped by NUM. I have a DataFrame, without groupings. Maybe I initially did not think correctly.