How to group a dataframe by a column?

Viewed 72

So I have this dataframe here. I want to make a pie chart of the countries that come up the most. In order for me to get my labeling correct, I need to create a list called 'countries' that begin with the country that appears most and end with countries that appear once. How would I do that exactly?

enter image description here

2 Answers

I am not sure I got you well, but maybe this is a solution:

df = sorted(your_df, key = your_df.loc[:, ["Nationality"]].count, reverse = True)

Your problem is something about sorting elements by frequency

Use Series.value_counts which sorted by default, so get indices and convert them to list:

countries = df["Nationality"].value_counts().index.tolist()
Related