2016-11-28 4 views
1

ich auf Datenrahmen zu arbeiten, ich versuche, die ich verwendet habe .STACK() FunktionWas ist der Datensatz Rückkehr aus dataframe.stack()

df = pd.read_csv('test.csv', usecols =['firstround','secondround','thirdround','fourthround','fifthround']) 

sortedArray = df.stack().value_counts() 

sortedArray = sortedArray.sort_index() 

Ich brauche die ersten Indexspaltenwerte und die 2. Index abgerufen werden Spaltenwerte aus dem sortedArray, was bedeutet, dass ich den x- und y-Wert aus dem sortierten Array benötige.

Irgendeine Idee, wie ich es tun kann?

Antwort

1

Ich glaube, Sie brauchen Series.iloc, weil Ausgabe von stack ist Series:

x = sortedArray.iloc[0] 
y = sortedArray.iloc[1] 

Probe:

df = pd.DataFrame({'A':['a','a','s'], 
        'B':['a','s','a'], 
        'C':['s','d','a']}) 

print (df) 
    A B C 
0 a a s 
1 a s d 
2 s a a 
sortedArray = df.stack().value_counts() 
print (sortedArray) 
a 5 
s 3 
d 1 
dtype: int64 

sortedArray = sortedArray.sort_index() 
print (sortedArray) 
a 5 
d 1 
s 3 
dtype: int64 

x = sortedArray.iloc[0] 
y = sortedArray.iloc[1] 

print (x) 
5 
print (y) 
1 

print (sortedArray.tolist()) 
[5, 1, 3] 

print (sortedArray.index.tolist()) 
['a', 'd', 's'] 
+0

, wie ich eine Reihe von Index abgerufen werden kann und eine Anordnung von der sortierte Iloc-Array obwohl? – Ugine

+0

Glaubst du 'x = sortedArray.iloc [: 2] .values' und' x = sortedArray.index [: 2] '? – jezrael

+0

oh ich meine wie ich brauche ein Array von (x) [a, d, s] und ein anderes Array von (y) [5,1,3] – Ugine