[Python] Filter pandas dataframe rows of the most common values in a column

Discussão em 'Python' iniciado por Stack, Abril 26, 2025.

  1. Stack

    Stack Membro Participativo

    I have a pandas dataframe

    import pandas as pd

    df =pd.DataFrame({'name':['john','joe','bill','richard','sam'],
    'cluster':['1','2','3','1','2']})


    df['cluster'].value_counts() will give the number of occurrences of items based on the column cluster.

    Is it possible to retain only the rows which have the maximum number of occurrences in the column cluster?

    The expected output is

    [​IMG]

    The cluster 1 and 2 have the same number of occurrences, so all the rows for cluster 1 and 2 need to be retained.

    Continue reading...

Compartilhe esta Página