finding id name of 5 most frequent value in a column in pandas

Viewed 45

We have a data which has column name "birth_country" i executed following code;

import pandas as pd
df=pd.read_csv("data.csv")
df['birth_country'].value_counts()[:5]

output:

United States of America     259
United Kingdom               85
Germany                      61
France                       51
Sweden                       29

I want my output to be look like;

United States of America
United Kingdom
Germany
France
Sweden

How to do it?

Like;

df['birth_country'].value_counts().idxmax()

gives output:

United States of America
1 Answers

For series by index values use:

pd.Series(df['birth_country'].value_counts()[:5].index)
Related