2017-02-24 4 views
1

Ich habe einen Datensatz in PythonPython Datenrahmen GROUPBY

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 

und ich möchte es in eine Struktur konvertieren wie diese

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] 

Die ursprünglichen Datensatz Millionen von Zeilen hat, so I don‘ Ich möchte eine for-Schleife versuchen. Ist es möglich, es mit einer Gruppe zu tun? oder eine bestehende Funktion?

Antwort

1

Versuchen Sie folgendes:

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

Ausgang:

       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] 
Verwandte Themen