Search code examples
pythonpandasdataframesortingdelimited

Sort string values delimited by commas in a column using pandas


I have this dataframe, and I'm looking a pythonic way using Pandas to sort these values. Column 2 is a string.

C1   C2
0    b,g,f
1    a,f,c
2    f,e,a,c

The Output should look like:

C1   C2
0    b,f,g
1    a,c,f
2    a,c,e,f

Solution

  • import pandas as pd
    
    data = [
        {
            "C1":0,
            "C2":"b,g,f"
        },
        {
            "C1":1,
            "C2":"c,b,a"
        },
        {
            "C1":2,
            "C2":"f,e,a,c"
        }
    ]
    df = pd.DataFrame.from_dict(data)
    
    df.C2 = df.C2.sort_values().apply(lambda x: ",".join(sorted(x.split(","))))
    print(df)