2016-07-29 12 views
1

Ich möchte doppelte Elemente in einem Dataset entfernen, indem Sie diejenigen mit dem höchsten Wert beibehalten. Jetzt bin ich mit Pandas:So entfernen Sie doppelte Werte in einem Dataset: python

c_maxes = hospProfiling.groupby(['Hospital_ID', 'District_ID'], group_keys=False)\ 
       .apply(lambda x: x.ix[x['Hospital_employees'].idxmax()]) 
print c_maxes 

c_maxes.to_csv('data/external/HospitalProfilingMaxes.csv') 

dies tun zu dem anfänglichen Datensatz führe: Hospital_ID,District_ID,Hospital_employeesHospital_ID,District_ID,Hospital_ID,District_ID,Hospital_employees werden.

Die zur Gruppierung verwendeten Spalten werden dupliziert. Was ist der Fehler hier?

Edit:

auf die groupby() Funktion verwenden, eine zusätzliche Spalte am Anfang der Daten hinzugefügt werden. Die Spalte hat keinen Namen, sie ist nur eine Sequenznummer für alle Zeilen. Dies wird in der Ausgabe der zweiten Antwort der Fragen hier gezeigt. Ich möchte diese zusätzliche Spalte entfernen, da ich sie nicht brauche. Ich versuchte dies:

hospProfiling.drop(hospProfiling.columns[0], axis=1)

Dieser Code entbindet nicht von der Spalte. Wie kann es entfernt werden?

Antwort

3

Warum nicht groupby mit max Methode?

hopsProfiling.groupby(['Hospital_ID','District_ID'],as_index = False).max() 

Und wenn Sie mehr als drei Spalten passieren müssen, ersetzen max durch agg:

hopsProfiling.groupby(['Hospital_ID','District_ID'],as_index = False).agg({'Hospital employees': max}) 
1

Ich glaube, Sie brauchen:

hospProfiling.loc[hospProfiling.groupby(['Hospital_ID', 'District_ID'])['Hospital_employees'] 
           .idxmax()] 

ich mit einer anderen Antwort war sehr überrascht, und ich einige der Forschung, wenn die Funktion idxmax nutzlos ist oder nicht:

Probe:

hospProfiling = pd.DataFrame({'Hospital_ID': {0: 'A', 1: 'A', 2: 'B', 3: 'A', 4: 'A', 5: 'B', 6: 'A', 7: 'A', 8: 'B', 9: 'B', 10: 'A', 11: 'B', 12: 'A'}, 'Name': {0: 'Sam', 1: 'Annie', 2: 'Fred', 3: 'Sam', 4: 'Annie', 5: 'Fred', 6: 'Sam', 7: 'Annie', 8: 'Fred', 9: 'James', 10: 'Alan', 11: 'Julie', 12: 'Greg'}, 'District_ID': {0: 'M', 1: 'F', 2: 'M', 3: 'M', 4: 'F', 5: 'M', 6: 'M', 7: 'F', 8: 'M', 9: 'M', 10: 'M', 11: 'F', 12: 'M'}, 'Hospital_employees': {0: 25, 1: 41, 2: 70, 3: 44, 4: 12, 5: 14, 6: 20, 7: 10, 8: 30, 9: 18, 10: 56, 11: 28, 12: 33}, 'Val': {0: 100, 1: 7, 2: 14, 3: 200, 4: 5, 5: 20, 6: 1, 7: 0, 8: 7, 9: 9, 10: 6, 11: 9, 12: 47}}) 
hospProfiling = hospProfiling[['Hospital_ID','District_ID','Hospital_employees','Val','Name']] 
hospProfiling.sort_values(by=['Hospital_ID','District_ID'], inplace=True) 
print (hospProfiling) 
    Hospital_ID District_ID Hospital_employees Val Name 
1   A   F     41 7 Annie 
4   A   F     12 5 Annie 
7   A   F     10 0 Annie 
0   A   M     25 100 Sam 
3   A   M     44 200 Sam 
6   A   M     20 1 Sam 
10   A   M     56 6 Alan 
12   A   M     33 47 Greg 
11   B   F     28 9 Julie 
2   B   M     70 14 Fred 
5   B   M     14 20 Fred 
8   B   M     30 7 Fred 
9   B   M     18 9 James 

Hauptunterschied ist, wie man mit anderen Spalten umgeht, wenn max verwendet wird, gibt Max-Werte von jeder Spalte zurück - h ere Hospital_employees und Val:

c_maxes = hospProfiling.groupby(['Hospital_ID','District_ID'],as_index = False).max() 
print (c_maxes) 
    Hospital_ID District_ID Hospital_employees Name Val 
0   A   F     41 Annie 7 
1   A   M     56 Sam 200 
2   B   F     28 Julie 9 
3   B   M     70 James 20 

c_maxes = hospProfiling.groupby(['Hospital_ID','District_ID'],as_index = False) 
         .agg({'Hospital_employees': max}) 
print (c_maxes) 
    Hospital_ID District_ID Hospital_employees 
0   A   F     41 
1   A   M     56 
2   B   F     28 
3   B   M     70 

Funktion idxmax return Indizes der Maximalwerte in einer anderen Spalte:

print (hospProfiling.groupby(['Hospital_ID', 'District_ID'])['Hospital_employees'].idxmax()) 
A   F    1 
      M    10 
B   F    11 
      M    2 
Name: Hospital_employees, dtype: int64 

Und dann wählen Sie nur DataFrame von loc:

c_maxes = hospProfiling.loc[hospProfiling.groupby(['Hospital_ID', 'District_ID'])['Hospital_employees'] 
         .idxmax()] 
print (c_maxes) 
    District_ID Hospital_ID Hospital_employees Name Val 
1   F   A     41 Annie 7 
10   M   A     56 Alan 6 
11   F   B     28 Julie 9 
2   M   B     70 Fred 14 
Verwandte Themen