I have a data frame roughly like this
data = [
{'user_id': 1, 'week': 1, 'score': 1},
{'user_id': 1, 'week': 2, 'score': 2},
{'user_id': 1, 'week': 2, 'score': 3},
{'user_id': 2, 'week': 1, 'score': 1},
{'user_id': 2, 'week': 1, 'score': 1}]
df = pd.DataFrame(data)
+---------+------+-------+
| user_id | week | score |
+---------+------+-------+
| 1 | 1 | 1 |
| 1 | 2 | 2 |
| 1 | 2 | 3 |
| 2 | 1 | 1 |
| 2 | 1 | 1 |
+---------+------+-------+
I want to group this by user_id
and week
, but then take each score in each group and pivot it into a new column, so that the resulting data frame looks like this:
+---------+------+--------+--------+
| user_id | week | score1 | score2 |
+---------+------+--------+--------+
| 1 | 1 | 1 | |
| 1 | 2 | 2 | 3 |
| 2 | 1 | 1 | 1 |
+---------+------+--------+--------+
The group-by is straightforward,
df.groupby(['user_id', 'week'], as_index=False)
but I cannot see how to do the reshaping