I have the following records grouped by the user_id and action columns.
user_id | action | count
1 | read | 15
1 | write | 5
1 | delete | 7
2 | write | 2
3 | read | 9
3 | write | 1
3 | delete | 2
I want to convert this table into the following format where each action is now a column and the rows are the count values.
user_id | read | write | delete
1 | 15 | 5 | 7
2 | 0 | 2 | 0
3 | 9 | 1 | 2
I know how to do this using loops but I am curious if there is a more efficient way of doing this in GraphLab create SFrame or Panda's DataFrame.
I appreciate any help!
You can use
pivot
withfillna
and last castfloat
toint
byastype
:Another solution with
set_index
andunstack
:Solution if duplicates in column
ser_id
andaction
andpivot
orunstack
cannot be used isgroupby
with aggregatingmean
orsum
and reshape byunstack
:Timings: