Home > Software design >  finding id name of 5 most frequent value in a column in pandas
finding id name of 5 most frequent value in a column in pandas

Time:02-08

Let's say we have a data which has coloumn 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, kindly looking for help.

Like;

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

gives OUTPUT:

United States of America

CodePudding user response:

For series by index values use:

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