[Python] Aggregate and Order Different String Columns uniquely on Pandas

Discussão em 'Python' iniciado por Stack, Novembro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I am facing a problem to get the unique values of two different columns ordered A-Z and ignoring nan values.
    I was trying to create an ordered list of unique values, then remove the brackets, but I struggle.
    Can someone please help me with this?

    Columns to order:

    • df['A']
    • df['B']

    Thanks in advance.

    Sample Data


    import pandas as pd
    dict = {'A': ['1','2','8','4',nan],
    'B': ['6','2','3','9','10']}
    df = pd.DataFrame(dict)

    Desired Output

    A B expected_col
    0 1 6 1, 6
    1 2 2 2
    2 8 3 3, 8
    3 4 9 4, 9
    4 nan 10 10

    Continue reading...

Compartilhe esta Página