2017-01-20 2 views
1

Ich habe ein Pandas Daten aus der folgenden IndizesNatürlich Pandas Daten rasies Fehler

print(df.index) 
MultiIndex(levels=[[u'Day 3', u'Day 4', u'Day 5', u'Day 7', u'Day 9'], [u'D1', u'D10', u'D11', u'D12', u'D2', u'D3', u'D4', u'D5', u'D6', u'D7', u'D8', u'D9'], [1.0, 2.0, 3.0]], 
     labels=[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4], [0, 0, 0, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 1, 1, 1, 2, 2, 2, 3, 3, 3, 0, 0, 0, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 1, 1, 1, 2, 2, 2, 3, 3, 3, 0, 0, 0, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 1, 1, 1, 2, 2, 2, 3, 3, 3, 0, 0, 0, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 1, 1, 1, 2, 2, 2, 3, 3, 3, 0, 0, 0, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 1, 1, 1, 2, 2, 2, 3, 3, 3], [1, 2, 0, 1, 2, 0, 0, 2, 1, 0, 1, 2, 2, 0, 1, 0, 2, 1, 1, 2, 0, 1, 0, 2, 2, 0, 1, 0, 1, 2, 2, 1, 0, 1, 2, 0, 0, 2, 1, 0, 2, 1, 2, 0, 1, 0, 2, 1, 1, 0, 2, 0, 1, 2, 0, 2, 1, 2, 0, 1, 0, 2, 1, 0, 2, 1, 2, 0, 1, 0, 2, 1, 2, 1, 0, 0, 2, 1, 1, 2, 0, 0, 2, 1, 0, 1, 2, 0, 1, 2, 2, 1, 0, 1, 0, 2, 1, 0, 2, 0, 1, 2, 2, 0, 1, 1, 0, 2, 1, 2, 0, 1, 1, 2, 0, 2, 1, 0, 1, 2, 0, 0, 1, 2, 0, 1, 2, 2, 1, 0, 1, 0, 2, 2, 0, 1, 0, 1, 2, 0, 2, 1, 2, 0, 1, 1, 2, 0, 0, 2, 1, 0, 2, 1, 0, 2, 1, 2, 1, 0, 0, 2, 1, 2, 0, 1, 2, 0, 1, 2, 1, 0, 1, 2, 0, 2, 1, 0, 1, 2, 0]], 
     names=[u'Interval', u'Device', u'Well']) 

I Sortierung bin mit den folgenden

df = df.reindex(index=natsorted(df.index)) 

Mit diesem speziellen df jedoch das Sortieren, gibt sie die Folge Error.

raise Exception("cannot handle a non-unique multi-index!") 
Exception: cannot handle a non-unique multi-index! 

Jede Hilfe wäre sehr dankbar.

+1

Könnten Sie ein Minimal, komplette bieten und prüfbare Beispiel http://stackoverflow.com/help/mcve? was bedeutet, dass ich ein Skript kopieren/löschen und lokal ausführen kann. – ppasler

+0

Das ist schwierig, weil Daten aus einer Reihe von Excel-Dateien in Pandas eingelesen werden. Ich hatte gehofft, dass ich etwas Offensichtliches vermisste – agf1997

+0

Bitte sehen Sie mein Beispiel und kommentieren Sie, wenn es Ihren Eingabedaten entspricht. – ppasler

Antwort

1

Ich machte ein minimales Beispiel und konnte Ihren Fehler reproduzieren. Es scheint, dass es passiert, wegen der gleichen Ebenen Tuple Day 3, D1 and 1.0 in arrays. Wenn Sie einen von ihnen entfernen, funktioniert es gut.

import pandas as pd 
import numpy as np 
from natsort import natsorted 

arrays = [[u'Day 3', u'Day 3', u'Day 4', u'Day 5', u'Day 7', u'Day 9', u'Day 3', u'Day 4', u'Day 5', u'Day 7', u'Day 9'], 
      [u'D1', u'D1', u'D10', u'D11', u'D12', u'D2', u'D3', u'D4', u'D5', u'D6', u'D7'], 
      [1.0,  1.0,  2.0,  3.0,  1.0,  2.0,  1.0,  2.0,  3.0,  1.0,  2.0]] 
tuples = list(zip(*arrays)) 
index = pd.MultiIndex.from_tuples(tuples, names=[u'Interval', u'Device', u'Well']) 
df = pd.Series(np.random.randn(len(arrays[0])), index=index) 

print df.index 

df = df.reindex(index=natsorted(df.index)) 

Wie Sie erwähnten Sie mehrere Excel-Dateien verwenden, kann dies hilfreich sein: Merging multiple dataframes with non unique indexes