How to Sort Dataframe by a List in Python

If you found yourself doing repetitive work, automate it. This is a case I came across more than once recently — thought it’s worth sharing with who might have a similar problem.

The problem: Your output (name+ranking) is sorted by the namecolumn alphabetically. Your stakeholder needs the data frame to be sorted by the order of the key column specified by them.

The solution

  1. Transform the sort_key and the original output into data frames.

2. Do an outer join using pd.merge

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store