0

I have a pandas data frame with 3 columns: id, start_time, channel. for example:

id start_time channel
a b c
a b d
a f g
e h i

etc..

I would like to compact the table and have something like this:

id start_time channel
a [b,f] [c,d,g]
e h i

(The id has to be unique value). For every id can I have one or more start_time and channel. I appreciate any help

  • Does this answer your question? [How to get unique values from multiple columns in a pandas groupby](https://stackoverflow.com/questions/36106490/how-to-get-unique-values-from-multiple-columns-in-a-pandas-groupby) – BloomShell Oct 06 '22 at 10:43

1 Answers1

1

Use unique after groupby:

df.groupby('id', as_index=False).agg(lambda x: x.unique())

Output:

   id   start_time  channel
0   a   [b, f]      [c, d, g]
1   e   [h]          [i]
Nuri Taş
  • 3,828
  • 2
  • 4
  • 22