Ugine Ugine - 12 days ago 8
Python Question

What is the dataset return from dataframe.stack()

I am trying to work on dataframe which i have used .stack() function

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

sortedArray = df.stack().value_counts()

sortedArray = sortedArray.sort_index()


I need to retrieve the first index column values and the 2nd index column values from the sortedArray, meaning i need x and y value from the sorted array.

Any idea how i can do it?

Answer

I think you need Series.iloc, because output from stack is Series:

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

Sample:

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']