Grouping dataframe into list

  list, pandas, pandas-groupby, python

I have a pandas dataframe like:

**CATEGORY  USER**
A   Alfa
A   Bravo
B   Charlie
B   Delta
C   Delta
D   Echo
C   Foxtrot
E   Foxtrot
D   Golf
F   Hotel
F   India
G   Juliett
H   Kilo

I want to group by the first column and get second column as lists in rows:

**CATEGORY  USER    RELATION Lv. 1  *RELATION Lv. 2***
A   Alfa    [Alfa, Bravo]   [Alfa, Bravo]
A   Bravo   [Alfa, Bravo]   [Alfa, Bravo]
B   Charlie [Charlie, Delta]    [Charlie, Delta, Foxtrot]
B   Delta   [Charlie, Delta]    [Charlie, Delta, Foxtrot]
C   Delta   [Delta, Foxtrot]    [Charlie, Delta, Foxtrot]
D   Echo    [Echo, Golf]    [Echo, Golf]
C   Foxtrot [Delta, Foxtrot]    [Charlie, Delta, Foxtrot]
E   Foxtrot [Foxtrot]   [Charlie, Delta, Foxtrot]
D   Golf    [Echo, Golf]    [Echo, Golf]
F   Hotel   [Hotel, India]  [Hotel, India]
F   India   [Hotel, India]  [Hotel, India]
G   Juliett [Juliett]   [Juliett]
H   Kilo    [Kilo]  [Kilo]

Is it possible to do something like this using pandas groupby?

Source: Python Questions

LEAVE A COMMENT