2017-01-31 4 views
3

Mein df: using Spalte p234_r_cwählen größte N einer Spalte jeder groupby Gruppe Pandas

{'city1': {0: 'Chicago', 
    1: 'Chicago', 
    2: 'Chicago', 
    3: 'Chicago', 
    4: 'Miami', 
    5: 'Houston', 
    6: 'Austin'}, 
'city2': {0: 'Toronto', 
    1: 'Detroit', 
    2: 'St.Louis', 
    3: 'Miami', 
    4: 'Dallas', 
    5: 'Dallas', 
    6: 'Dallas'}, 
'p234_r_c': {0: 5.0, 1: 4.0, 2: 2.0, 3: 0.5, 4: 1.0, 5: 4.0, 6: 3.0}, 
'plant1_type': {0: 'COMBCYCL', 
    1: 'COMBCYCL', 
    2: 'NUKE', 
    3: 'COAL', 
    4: 'NUKE', 
    5: 'COMBCYCL', 
    6: 'COAL'}, 
'plant2_type': {0: 'COAL', 
    1: 'COAL', 
    2: 'COMBCYCL', 
    3: 'COMBCYCL', 
    4: 'COAL', 
    5: 'NUKE', 
    6: 'NUKE'}} 

Ich möchte 2 groupby Operationen tun, und die größte 1 jeder Gruppe zu nehmen.

1. groupby = ['plant1_type', 'plant2_type', 'city1']

2. groupby = ['plant1_type', 'plant2_type', 'city2']

Als solche ich folgendes:

df.groupby(['plant1_type','plant2_type','city1'])['p234_r_c'].\ 
    nlargest(1).reset_index() 


plant1_type plant2_type city1 level_3 p234_r_c 
0 COAL COMBCYCL Chicago 3 0.5 
1 COAL NUKE  Austin 6 3.0 
2 COMBCYCL COAL Chicago 0 5.0 
3 COMBCYCL NUKE Houston 5 4.0 
4 NUKE COAL  Miami 4 1.0 
5 NUKE COMBCYCL Chicago 2 2.0 

Das Ergebnis des ersten groupby Sinn macht. Allerdings bin ich durch das Ergebnis des 2. groupby verwirrt:

df.groupby(['plant1_type','plant2_type','city2'])['p234_r_c'].\ 
    nlargest(1).reset_index() 

index p234_r_c 
0 0 5.0 
1 1 4.0 
2 2 2.0 
3 3 0.5 
4 4 1.0 
5 5 4.0 
6 6 3.0 

Was Spalten passiert plant1_type, plant2_type und city2 im Ergebnis? Sollten sie nicht im Ergebnis erscheinen, so wie plant1_type, plant2_type und city1 im Ergebnis der ersten groupby erschienen?

+3

Es ist wahrscheinlich zurückkehren, dass Sie einen Fehler – Boud

+0

Gut finden gefunden! @codingknob – piRSquared

Antwort

4

I added an issue here

Theorie:

Wenn die Ergebnisse eines groupby auf einem pd.Series die gleichen pd.Series Werte zurückgibt, dann wird der ursprüngliche Index zurückgegeben.

unten Beispiel

df = pd.DataFrame(dict(A=[0, 1, 2, 3])) 

# returns results identical to df.A 
print(df.groupby(df.A // 2).A.nsmallest(2)) 

# returns results out of order 
print(df.groupby(df.A // 2).A.nlargest(2)) 

0 0 
1 1 
2 2 
3 3 
Name: A, dtype: int64 
A 
0 1 1 
    0 0 
1 3 3 
    2 2 
Name: A, dtype: int64 

Gekochte Ich würde argumentieren, dass Sie diese den gleichen konsistenten Index zurückkehren möchten.

Dies ist die ungeheuerlichsten Folge davon ist:

A 
0 1 1 
    0 0 
1 2 2 
    3 3 
Name: A, dtype: int64 

Und dies auf einem anderen

0 0 
1 1 
2 2 
3 3 
Name: A, dtype: int64 

Natürlich

# most egregious 
# this will be randomly different 
print(df.groupby(df.A // 2).A.apply(pd.Series.sample, n=2)) 

gibt diese an einer Ausführung dieser nie hat ein Problem, weil es " s unmöglich die gleichen Werte wie das Original

print(df.groupby(df.A // 2).A.apply(pd.Series.sample, n=1)) 

A 
0 0 0 
1 2 2 
Name: A, dtype: int64 

Umgehen
set_index

cols = ['plant1_type','plant2_type','city2'] 
df.set_index(cols).groupby(level=cols)['p234_r_c'].\ 
    nlargest(1).reset_index() 

    plant1_type plant2_type  city2 p234_r_c 
0 COMBCYCL  COAL Toronto  5.0 
1 COMBCYCL  COAL Detroit  4.0 
2  NUKE COMBCYCL St.Louis  2.0 
3  COAL COMBCYCL  Miami  0.5 
4  NUKE  COAL Dallas  1.0 
5 COMBCYCL  NUKE Dallas  4.0 
6  COAL  NUKE Dallas  3.0 
+1

Schöne Forschung, mit anderen Worten, wenn keine Aggregation dann Fehler - 'df.groupby (['plant1_type', 'plant2_type', 'city1']) ['p234_r_c']. \ nlargest (2) .reset_index() ', aber wenn' 1' dann funktioniert - 'df.groupby (['plant1_type', 'plant2_type', 'city1']) ['p234_r_c']. \ nlargest (1) .reset_index() '- es gibt eine Aggregation. – jezrael

+0

Ja! Genau richtig. – piRSquared

+0

Fellas, gibt es in der Zwischenzeit eine Möglichkeit, den Fehler zu umgehen? – codingknob

Verwandte Themen