[Python] Sorting of categorical variables using np.unique

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

  1. Stack

    Stack Membro Participativo

    I'm trying to get the unique values of categorical variables in sorted fashion using the below code but without success.

    import numpy as np

    unique_values, unique_value_counts = np.unique(['Small', 'Medium', 'Large', 'Medium', 'Small', 'Large', 'Small', 'Medium'], return_counts = True)

    print(unique_values)


    which gives me an output like below

    ['Large', 'Medium', 'Small']


    However, I'm expecting output in ascending format like

    ['Small', 'Medium', 'Large']


    Is there a way wherein I can get the categorical values in a sorted format using np.unique()?

    Continue reading...

Compartilhe esta Página