Ryan Kuhle
Ryan Kuhle

Reputation: 1

Python pandas: determining which "group" has the most entries

Let's say that I have pandas DataFrame with a column called "fruit" that represents what fruit my classroom of kindergartners had for a morning snack. I have 20 students in my class. Breakdown would be something like this.

Oranges = 7, Grapes = 3, Blackberries = 4, Bananas = 6

I used sort to group each of these fruit types, but it is grouping based on alphabetical order. I would like it to group based on the largest quantity of entries for that class of fruit. In this case, I would like Oranges to turn up first so that I can easily see that Oranges is the most popular fruit.

I'm thinking that sort is not the best way to go about this. I checked out groupby but could not figure out how to use that appropriately either.

Thanks in advance.

Upvotes: 0

Views: 113

Answers (2)

Alexander
Alexander

Reputation: 109626

In Pandas 0.17:

df.sort_values(by='fruit', ascending=False)

Upvotes: 0

maxymoo
maxymoo

Reputation: 36545

To sort by name: df.fruit.value_counts().sort_index()

To sort by counts: df.fruit.value_counts().sort_values()

Upvotes: 2

Related Questions