DEV Community

Nilotpal Choudhury
Nilotpal Choudhury

Posted on

Answer: How to sort pandas dataframe by custom order on string index

As of Pandas 1.1 DataFrame.sort_values has a key param that takes a callable to control sorting. So you could use an approach like the following:

def sorter(column)
    reorder = [
        "Maurice Baker",
        "Adrian Caldwell",
        "Ratko Varda",
        "Ryan Bowen",
        "Cedric Hunter",
    ]
    # This also works:
    # mapper = {name: order

Top comments (0)