"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How Can I Custom Sort a Pandas DataFrame Column Based on a Dictionary?

How Can I Custom Sort a Pandas DataFrame Column Based on a Dictionary?

Posted on 2025-03-25
Browse:839

How Can I Custom Sort a Pandas DataFrame Column Based on a Dictionary?

Custom Sorting in Pandas Dataframes

In Pandas, sometimes you may need to sort a dataframe based on a custom order. This can be achieved using a dictionary to define the desired sorting order.

Problem:
You have a Pandas dataframe with a column containing month names. You want to sort this column using a custom dictionary, such as:

custom_dict = {'March':0, 'April':1, 'Dec':3}

Solution:

Using Categorical Series:
Pandas 0.15 introduced Categorical Series, which provides an elegant way to handle this scenario:

  1. Convert the month column into a categorical series, specifying the custom ordering:

    df['m'] = pd.Categorical(df['m'], ["March", "April", "Dec"])
  2. Sort the dataframe based on the categorical column:

    df.sort_values("m")

Using an Intermediary Series:
Prior to Pandas 0.15, you could utilize an intermediary series to achieve custom sorting:

  1. Apply the custom dictionary to the month column:

    s = df['m'].apply(lambda x: {'March':0, 'April':1, 'Dec':3}[x])
  2. Sort the intermediary series:

    s.sort_values()
  3. Set the index of the dataframe to the sorted intermediary series and sort:

    df.set_index(s.index).sort()

Using the Replace Method:
In newer versions of Pandas, Series offers a replace method that allows for a more concise solution:

df['m'].replace({'March':0, 'April':1, 'Dec':3})

This method replaces the month values with the corresponding sorting values specified in the dictionary. Sorting the dataframe based on the modified month column will achieve the desired custom order.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3