i have a dataframe
id  name value    flag
1    a     x        F
1    b     y        A
2    c     z        B
3    d     m        Q
if i want to groupby id and put value column into a new column as a list.
i can do
df.groupby('id')['value'].apply(list).reset_index()
is there any way where i can do groupby by 'id' but put 2 column's(name and value) into list.
my desired output
id    col
 1    [[a,x],[b,y]]
 2    [[c,z]]
 3    [[d,m]]
                Convert columns to numpy array by values and then to lists in groupby or sepearately to new Series:
df = df.groupby('id')
       .apply(lambda x: x[['name','value']].values.tolist())
       .reset_index(name='col')
print (df)
   id               col
0   1  [[a, x], [b, y]]
1   2          [[c, z]]
2   3          [[d, m]]
Or:
s = pd.Series(df[['name','value']].values.tolist(), index=df.index)
df = s.groupby(df['id']).apply(list).reset_index(name='col')
print (df)
   id               col
0   1  [[a, x], [b, y]]
1   2          [[c, z]]
2   3          [[d, m]]
Also if no problem with tuples in lists:
s = pd.Series(list(zip(df['name'],df['value'])), index=df.index)
df = s.groupby(df['id']).apply(list).reset_index(name='col')
print (df)
   id               col
0   1  [(a, x), (b, y)]
1   2          [(c, z)]
2   3          [(d, m)]
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With