Python - Unsure how to roll up row values within a column into a list

Viewed 790

Here is an example:

df1:

X Y Z
1 a cat
1 a dog
2 b hi
2 b hello
2 b hey

The final df should look like this.

df2:

X Y Z
1 a [cat, dog]
2 b [hi, hello, hey]

I'm really stuck and I'm having trouble even approaching this. Any help would be much appreciated.

1 Answers

You can use groupby and apply:

df.groupby(['X', 'Y'])['Z'].apply(list)

returns

X  Y
1  a          [cat, dog]
2  b    [hi, hello, hey]
Name: Z, dtype: object

Edit: can just apply list instead of Series.tolist as @timegb suggests in the comments.

Related