[Python] Grouped by counts in Python

Discussão em 'Python' iniciado por Stack, Março 5, 2025.

  1. Stack

    Stack Membro Participativo

    We have a df called allyears that has years, gender, names in it.

    Something like this:

    name sex number year
    John M 1 2010
    Jane F 2 2011

    I want to group the top10 names for a given year with their respective counts. I tried this code, but this is not returning what I am looking for.

    males = allyears[(allyears.year>2009)&(allyears.sex=='M')]
    maleNameCounts = pd.DataFrame(males.groupby(['year', 'name']).count())
    maleNameCounts.sort_values('number', ascending=True)


    How should I be approaching this problem?

    Continue reading...

Compartilhe esta Página