pandas - sorting a dataframe by values and storing index and columns -
i have pandas dataframe matrix. looks shown below
b c d 1 0 5 e 0 6 2 f 2 0 3
i need values sorted , need values of index , columns of them. result should be
index column value e b 6 d c 5 f c 3
you need stack
reshape nlargest
:
df1 = df.stack().nlargest(3).rename_axis(['idx','col']).reset_index(name='val') print (df1) idx col val 0 e b 6 1 d c 5 2 f c 3
for multiindex
:
df2 = df.stack().nlargest(3).to_frame(name='val') print (df2) val e b 6 d c 5 f c 3
Comments
Post a Comment