We can use the groupby()
method of pandas to group the data by a particular column and the sort_values()
method to sort it in descending order. Here's an example:
import pandas as pd
# create sample data
data = {'fruit': ['apple', 'banana', 'banana', 'apple', 'banana'],
'count': [2, 3, 1, 4, 2]}
df = pd.DataFrame(data)
# group by 'fruit' column and sum the 'count' values
grouped = df.groupby('fruit').sum()
# sort by 'count' values in descending order
sorted_grouped = grouped.sort_values('count', ascending=False)
print(sorted_grouped)
Output:
count
fruit
apple 6
banana 6
Asked: 2022-01-25 11:00:00 +0000
Seen: 20 times
Last updated: Oct 13 '21