Abhinav Bajpai
Abhinav Bajpai

Reputation: 451

Python DataFrame Groupby

I have a data set in Python

 A   B      C     D    E
 0  foo    one  0.46 -0.86
 1  bar    one -0.28 -2.10
 2  foo    two -1.50 -0.49
 3  bar  three -1.13  1.07
 4  foo    two  1.21  0.72
 5  bar    two -0.17 -0.70
 6  foo    one  0.11 -1.03
 7  foo  three -1.04  0.27

and I want to convert it into a structure like this

 A   B          C                D               E
 0   foo   [one,two,two]   [.46,-1.50,1.2]   [-.86,-.49,.72]
 1   bar   [one,three,two] [-.28,-1.13,-.17] [-2.1,1.07,-.70]

The original data set has million of rows so I don't want to attempt a for loop. Is it possible to do it using a groupby ? or an existing function?

Upvotes: 1

Views: 313

Answers (1)

Alex Fung
Alex Fung

Reputation: 2006

Try this :

f = lambda x: x.tolist()
df1 = df.groupby('B').agg({'C':f,'D':f,'E':f})
print(df1)

Output:

                               C                                  E  \
B                                                                     
bar            [one, three, two]                 [-2.1, 1.07, -0.7]   
foo  [one, two, two, one, three]  [-0.86, -0.49, 0.72, -1.03, 0.27]   

                                   D  
B                                     
bar            [-0.28, -1.13, -0.17]  
foo  [0.46, -1.5, 1.21, 0.11, -1.04]  

Upvotes: 1

Related Questions