[Python] as_index=False groupBy doesn't work with count

Discussão em 'Python' iniciado por Stack, Setembro 1, 2025.

  1. Stack

    Stack Membro Participativo

    When I run groupby on a dataframe with as_index set to False, count seems to not work at all. For example,

    import pandas as pd

    word_list = ['a', 'b', 'c', 'a', 'c', 'c', 'b', 'a', 'c']

    df = pd.DataFrame(word_list, columns=['word'])
    as_counts = df.groupby('word', as_index=False).count()
    print(as_counts)


    word
    0 a
    1 b
    2 c


    If you try the same with .size() as in

    as_counts = df.groupby('word', as_index=False).size()


    it works as expected

    word size
    0 a 3
    1 b 2
    2 c 4


    What difference between the two functions explains this difference in output?

    This post is asking a different question entirely as, in this case, the frame is neither MultiIndex nor seeking to maintain non-unique indices. Equally, none of the answers in that post address the problem of this question.

    Continue reading...

Compartilhe esta Página