Kepp biggest values for each column of the pandas.DataFrame respecting indexes

Viewed 20

my expectation that

cheer_leading.nlargest(10, s_cols, keep='all') 

will return top performers for each column, but in reality, it returns only performers in the first columns. So to get the largest in every category I have to iterate through columns and save indexes manually.

top_ = set()
for c in s_cols:
    top_ |= set(cheer_leading[c].nlargest(10).index)
    
cheer_leading.loc[top_]

is there an easier way to achieve the same?

1 Answers

Try with

out = df.apply(lambda x : x.nlargest(2).index)
Related