[Python] Pandas Dataframe unique values per group based on label [duplicate]

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

  1. Stack

    Stack Membro Participativo

    I have a dataframe with two labels e.g.:

    df = pd.DataFrame({'Brand': ['VW', 'VW', 'BMW', 'BMW', 'Mercedes'],
    'color': ['red', 'red', 'red', 'blue', 'black']})

    >>df

    Brand color
    0 VW red
    1 VW red
    2 BMW red
    3 BMW blue
    4 Mercedes black


    For each unique item in the first label ('brand') I want a list of all unique values of the second label ('color'). So the output would look like:

    Brand colors
    0 VW [red]
    1 BMW [red, blue]
    2 Mercedes [black]

    Continue reading...

Compartilhe esta Página