Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groupby one column and apply 2 columns into list pandas

Tags:

python

pandas

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]]
like image 344
Shubham R Avatar asked Mar 07 '23 11:03

Shubham R


1 Answers

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)]
like image 191
jezrael Avatar answered Apr 30 '23 17:04

jezrael