Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9007091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:35:33+00:00 2026-06-16T01:35:33+00:00

I have python pandas dataframe, in which a column contains month name. How can

  • 0

I have python pandas dataframe, in which a column contains month name.

How can I do a custom sort using a dictionary, for example:

custom_dict = {'March':0, 'April':1, 'Dec':3}  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:35:35+00:00Added an answer on June 16, 2026 at 1:35 am

    Pandas 0.15 introduced Categorical Series, which allows a much clearer way to do this:

    First make the month column a categorical and specify the ordering to use.

    In [21]: df['m'] = pd.Categorical(df['m'], ["March", "April", "Dec"])
    
    In [22]: df  # looks the same!
    Out[22]:
       a  b      m
    0  1  2  March
    1  5  6    Dec
    2  3  4  April
    

    Now, when you sort the month column it will sort with respect to that list:

    In [23]: df.sort_values("m")
    Out[23]:
       a  b      m
    0  1  2  March
    2  3  4  April
    1  5  6    Dec
    

    Note: if a value is not in the list it will be converted to NaN.


    An older answer for those interested…

    You could create an intermediary series, and set_index on that:

    df = pd.DataFrame([[1, 2, 'March'],[5, 6, 'Dec'],[3, 4, 'April']], columns=['a','b','m'])
    s = df['m'].apply(lambda x: {'March':0, 'April':1, 'Dec':3}[x])
    s.sort_values()
    
    In [4]: df.set_index(s.index).sort()
    Out[4]: 
       a  b      m
    0  1  2  March
    1  3  4  April
    2  5  6    Dec
    

    As commented, in newer pandas, Series has a replace method to do this more elegantly:

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

    The slight difference is that this won’t raise if there is a value outside of the dictionary (it’ll just stay the same).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dataframe generated from Python's Pandas package. How can I generate heatmap
I have a Python pandas DataFrame rpt : rpt <class 'pandas.core.frame.DataFrame'> MultiIndex: 47518 entries,
I have a dataframe in python pandas with several columns taken from a CSV
I have python-django site which contains css and js files. For every time of
New to pandas python. I have a dataframe (df) with two columns of cusips.
I have python script which downloads N number of images from website. I run
I have Python classes, of which I need only one instance at runtime, so
I have some values in a Python Pandas Series (type: pandas.core.series.Series ) In [1]:
I have python threaded application + Postgres. I am using Django's ORM to save
I have a pandas dataframe consisting of 23 series with a default sequential index

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.