Caterina
Caterina

Reputation: 987

Select k rows with the highest value of a given column

Let's suppose you have a pandas dataframe with col1 and you want to keep only the k samples with the highest value of col1. How can you do that?

Notice I'm not saying maximum value. But rather like sorting by col1, keeping the best k samples, and removing the rest.

Upvotes: 0

Views: 112

Answers (1)

Naveed
Naveed

Reputation: 11650

k=10 # some number
df.sort_values('col', ascending=False).head(k)

Upvotes: 1

Related Questions