Home > Blockchain >  How can I compact info in pandas
How can I compact info in pandas

Time:10-07

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

CodePudding user response:

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]
  • Related