简体   繁体   中英

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

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

For series by index values use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM