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