Finding highest n values of every column in dataframe [duplicate]
You can use apply
with pandas.Series.nlargest
function.
df.apply(lambda x: pd.Series(x.nlargest(3).index)) u1 u2 u30 q5 q1 NaN1 q4 q5 NaN2 q2 q4 NaN
We need do stack
then sort_values
and create the new index use cumcount
, and pivot
to reshape it back
s=df.stack().reset_index().sort_values(0,ascending=False).\ assign(index=lambda x : x.groupby('level_1').cumcount()).\ pivot('index','level_1','level_0').\ reindex(columns=df.columns).head(3)Out[308]: u1 u2 u3index 0 q5 q1 NaN1 q4 q5 NaN2 q2 q4 NaN