This is a sample of my dataset:
Consumer_num | billed_units
29 | 984
29 | 1244
29 | 2323
29 | 1232
29 | 1150
30 | 3222
30 | 1444
30 | 2124
I want to group by user_num and then add all the values (billed_units) of each group to the new columns. So my required result is:
Consumer_num | month 1 | month 2 | month 3 | month 4 | month 5
29 | 984 | 1244 | 2323 | 1232 | 1150
30 | 3222 | 1444 | 2124 | NaN | NaN
This is what I have done so far:
group = df.groupby('consumer_num')['billed_units'].unique()
group[group.apply(lambda x: len(x)>1)]
df = group.to_frame()
print df
Conclusion:
Consumer_num | billed_units
29 | [984,1244,2323,1232,1150]
30 | [3222,1444,2124]
I do not know if my approach is right. If this is correct, then I would like to know how I can separate the billed_units of each consumer, and then add to the new columns, as I showed in my required output. Or is there a better method to achieve my desired result?
user7018778
source
share