1

Starting from a dataframe of the form

userId  | tag_id
----------------
12         2
34         2
44         2
43         2
12         5
44         5
43         5
12         8
43         8
34         8
34         8
...

How can I pivot this table such that each value of tag_id becomes a new column, the rows of the table are unique userIDs and the values on each tag_id column is the number of times that user appears with the corresponding tag. The end result, for the above case should be:

userId | 2 | 5 | 8 |
--------------------
12       1   1   1   
34       1   0   2
43       1   1   1
44       1   1   0

I have a rough idea on how pivoting works, but I can't figure out how to do the counts inside it.

Qubix
  • 4,161
  • 7
  • 36
  • 73

0 Answers0