Home >Backend Development >Python Tutorial >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:
Convert the month column into a categorical series, specifying the custom ordering:
df['m'] = pd.Categorical(df['m'], ["March", "April", "Dec"])
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:
Apply the custom dictionary to the month column:
s = df['m'].apply(lambda x: {'March':0, 'April':1, 'Dec':3}[x])
Sort the intermediary series:
s.sort_values()
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.
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!