Home > Backend Development > Python Tutorial > 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?

Linda Hamilton
Release: 2024-11-27 19:30:14
Original
334 people have browsed it

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}
Copy after login

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"])
    Copy after login
  2. Sort the dataframe based on the categorical column:

    df.sort_values("m")
    Copy after login

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])
    Copy after login
  2. Sort the intermediary series:

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

    df.set_index(s.index).sort()
    Copy after login

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})
Copy after login

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.

The above is the detailed content of How Can I Custom Sort a Pandas DataFrame Column Based on a Dictionary?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template