Finding highest n values of every column in dataframe

Viewed 88

I want to find the highest 3 values of each column in a dataframe, and return the index names, ordered by value. The dataframe looks like this:

df = pd.DataFrame({"u1":[1,2,-3,4,5],
                   "u2":[8,-4,5,6,7],
                   "u3":[np.NaN,np.NaN,np.NaN,np.NaN,np.NaN]},
                   index=["q1","q2","q3","q4","q5"])

The result would look like this:

u1   u2   u3
q5   q1   NaN
q4   q5   NaN
q2   q4   NaN
2 Answers

You can use apply with pandas.Series.nlargest function.

df.apply(lambda x: pd.Series(x.nlargest(3).index))
   u1  u2   u3
0  q5  q1  NaN
1  q4  q5  NaN
2  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  u3
index            
0      q5  q1 NaN
1      q4  q5 NaN
2      q2  q4 NaN
Related