limit amount of rows as result of groupby Pandas

Viewed 588

My groupby code works fine:

df_20.groupby(['Jaar'])['GemeenteNaam'].value_counts()

It groups year 2015, 2016, 2017, 2018. How often a GemeenteNaam (in English: city name) is found in the column ['GemeenteNaam'] is given for every year with value_counts().

But this results in over 100 city's per year in my groupby result. I only want 10 city's per year.

Question: how can I limit the rows as a result of the groupby?

I tried this, but this of coure limits the total result:

df_20.groupby(['Jaar'])['GemeenteNaam'].value_counts().head(10)

I checked the groupby docstring, but couldn't find an answer. Hopefully you are smarter then I am (probably...). Thanks in advance!! greetings Jan (from rainy Netherlands).

2 Answers

You can filter values in lambda function:

df_20.groupby(['Jaar'])['GemeenteNaam'].apply(lambda x: x.value_counts().head(10))

Or add another groupby per level=0 - by first level of index:

df_20.groupby(['Jaar'])['GemeenteNaam'].value_counts().groupby(level=0).head(10)

Instead head(10) you can slice the first rows by:

head(10) ==> [:10]

Related