Home > Blockchain >  How to group a dataframe by a column?
How to group a dataframe by a column?

Time:12-21

So I have this dataframe here. I want to make a pie chart of 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 ending with countries that appear once. How would I do that exactly? enter image description here

CodePudding user response:

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

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

CodePudding user response:

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

  • Related