2

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.

madsthaks
  • 1,971
  • 4
  • 22
  • 41

1 Answers1

3

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.

Alex
  • 17,062
  • 7
  • 54
  • 78