2017-11-27 9 views
2

Lasst uns sagen, ich habe Daten des FormatsTransponieren und die Kombination von Panda Datenrahmen

option,subcase,prop1,prop2,prop3,... 

als

.csv gegeben

Ich möchte jetzt Statistiken für jeden option und zusätzliche Statistiken für jeden subcase auf seine eigene erstellen.

Wenn ich nur alles und waren nicht daran interessiert, Konfidenzintervall drucken wollte, wäre es wahrscheinlich so aussehen etwas:

import numpy as np 
import pandas as pd 
import sys 

df = pd.read_csv(sys.argv[1]) # note to self: argv[0] is script file content 

options = df.option.unique() 
option_data = {} 

subcases = df.subcase.unique() 
data = {} 

for o in options: 

    option_data[o] = df[df.option.apply(lambda row: o in row)] 
    print(o) 
    print(pd.DataFrame.describe(option_data[o])) 

    for s in subcases: 
     label = o + '_' + s 
     data[label] = option_data[o][option_data[o].subcase.apply(lambda row: s in row)]   
     print(label) 
     print(pd.DataFrame.describe(data[label])) 

    print() 

Dies ist allerdings furchtbar schwer zu lesen.

Wie kombiniere ich am besten die Datenframes s.t. Ich am Ende mit Bildern wie

prop1 mean std min 25% ... 
A 
A_a 
A_b 
A_c 
B 
B_a 
B_c 
... 

prop2 mean std min 25% ... 
A 
A_a 
A_b 
A_c 
B 
B_a 
B_c 
... 

Ich meine, ich konnte manuell Schleife durch alle Frames ... aber es hat etwas effizienter sein.

bearbeiten

Z.B.

option,subcase,cost,time 
A,sub1,4,3 
A,sub1,2,0 
A,sub2,3,8 
A,sub2,1,2 
B,sub1,13,0 
B,sub1,11,0 
B,sub2,5,2 
B,sub2,3,4 

sollten zwei Rahmen produzieren:

Kosten

,mean,std,min,25%,50%,75%,max 
A,2.5,1.290994,1,1.75,2.5,3.25,4 
A_sub1,3,1.414214,2,2.5,3,3.5,4 
A_sub2,2,1.414214,1,1.5,2,2.5,3 
B,8,4.760952,3,4.5,8,11.5,13 
B_sub1,12,1.414214,11,11.5,12,12.5,13 
B_sub2,4,1.414214,3,3.5,4,4.5,5 

und

Zeit

,mean,std,min,25%,50%,75%,max 
A,3.25,3.40343,0,1.5,2.5,4.25,8 
A_sub1,1.5,2.12132,0,0.75,1.5,2.25,3 
A_sub2,5,4.242641,2,3.5,5,6.5,8 
B,1.5,1.914854,0,0,1,2.5,4 
B_sub1,0,0,0,0,0,0,0 
B_sub2,3,1.414214,2,2.5,3,3.5,4 

Wo die Einträge für die A und B Zeilen werden basierend auf allen Einträgen ihrer entsprechenden Unterfälle berechnet.

+3

können Sie einen kleiner reproduzierbarer Probendatensatz und die entsprechenden gewünschte Daten eingestellt? – MaxU

+0

@MaxU Fertig, siehe oben Bearbeiten. – User1291

Antwort

2
In [79]: df.groupby(['option','subcase']).describe() 
Out[79]: 
       cost            time 
       count mean  std min 25% 50% 75% max count mean  std min 25% 50% 75% max 
option subcase 
A  sub1  2.0 3.0 1.414214 2.0 2.5 3.0 3.5 4.0 2.0 1.5 2.121320 0.0 0.75 1.5 2.25 3.0 
     sub2  2.0 2.0 1.414214 1.0 1.5 2.0 2.5 3.0 2.0 5.0 4.242641 2.0 3.50 5.0 6.50 8.0 
B  sub1  2.0 12.0 1.414214 11.0 11.5 12.0 12.5 13.0 2.0 0.0 0.000000 0.0 0.00 0.0 0.00 0.0 
     sub2  2.0 4.0 1.414214 3.0 3.5 4.0 4.5 5.0 2.0 3.0 1.414214 2.0 2.50 3.0 3.50 4.0 

UPDATE:

In [97]: r = df.groupby(['option','subcase']).describe() 

In [100]: t = df.groupby('option').describe().set_index(np.array([''] * df['option'].nunique()), append=True) 

In [101]: r.append(t).sort_index() 
Out[101]: 
       cost             time 
       count mean  std min 25% 50% 75% max count mean  std min 25% 50% 75% max 
option subcase 
A    4.0 2.5 1.290994 1.0 1.75 2.5 3.25 4.0 4.0 3.25 3.403430 0.0 1.50 2.5 4.25 8.0 
     sub1  2.0 3.0 1.414214 2.0 2.50 3.0 3.50 4.0 2.0 1.50 2.121320 0.0 0.75 1.5 2.25 3.0 
     sub2  2.0 2.0 1.414214 1.0 1.50 2.0 2.50 3.0 2.0 5.00 4.242641 2.0 3.50 5.0 6.50 8.0 
B    4.0 8.0 4.760952 3.0 4.50 8.0 11.50 13.0 4.0 1.50 1.914854 0.0 0.00 1.0 2.50 4.0 
     sub1  2.0 12.0 1.414214 11.0 11.50 12.0 12.50 13.0 2.0 0.00 0.000000 0.0 0.00 0.0 0.00 0.0 
     sub2  2.0 4.0 1.414214 3.0 3.50 4.0 4.50 5.0 2.0 3.00 1.414214 2.0 2.50 3.0 3.50 4.0 
+0

Ich mag diese Formatierung. Gibt es eine Möglichkeit, "A" und "B" einen "totalen" Subcase hinzuzufügen? – User1291

+0

@ User1291, überprüfen Sie bitte aktualisierte Antwort ... – MaxU

+0

wirklich schönes Format :-) – Wen

2

Durch die Verwendung von pd.concat

df1=df.groupby('option').cost.describe() 
df2=df.groupby(['option','subcase']).cost.describe() 

df2.index=df2.index.map('_'.join) 
pd.concat([df1,df2]).sort_index() 


Out[256]: 
     count mean  std min 25% 50% 75% max 
A   4.0 2.5 1.290994 1.0 1.75 2.5 3.25 4.0 
A_sub1 2.0 3.0 1.414214 2.0 2.50 3.0 3.50 4.0 
A_sub2 2.0 2.0 1.414214 1.0 1.50 2.0 2.50 3.0 
B   4.0 8.0 4.760952 3.0 4.50 8.0 11.50 13.0 
B_sub1 2.0 12.0 1.414214 11.0 11.50 12.0 12.50 13.0 
B_sub2 2.0 4.0 1.414214 3.0 3.50 4.0 4.50 5.0 
Verwandte Themen