1

I have the below table:

+-----------+------+-------+
| member_id | year | count |
+-----------+------+-------+
|         1 | 2012 |     1 |
|         1 | 2013 |     0 |
|         1 | 2014 |     1 |
|         2 | 2012 |     2 |
|         2 | 2013 |     0 |
|         2 | 2014 |     1 |
+-----------+------+-------+

and i'd like to turn it into this:

+-----------+------------+------------+------------+
| member_id | count_2012 | count_2013 | count_2014 |
+-----------+------------+------------+------------+
|         1 |          1 |          0 |          1 |
|         2 |          2 |          0 |          1 |
+-----------+------------+------------+------------+

I tried grouping and then pivoting and also just pivoting, but the pivot requires and aggfunc, and I'm not trying to change the values, just reshape it.

metersk
  • 11,803
  • 21
  • 63
  • 100

1 Answers1

1
df = df.pivot(index='member_id',columns='year', values='count')

If you dont want to have member_id as index:

df = df.pivot(index='member_id',columns='year', values='count').reset_index()