How to Sort Dataframe by a List in Python

Chi
Apr 14, 2021

--

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.
import pandas as pd sort_key=['Apple','Alphabet','Microsoft','Samsung','Huawei','IBM','Facebook','Cisco Systems','HP Inc.','Intel','Dell','Xiaomi','Oracle','SAP','Hitachi','Salesforce','Uber']data=['Apple','Alphabet','Cisco Systems','Dell','Facebook','Hitachi','HP Inc.','Huawei','IBM','Intel','Microsoft','Oracle','Salesforce','Samsung','SAP','Uber','Xiaomi']ranking=[2,1,12,20,10,29,15,6,8,19,4,25,35,5,27,37,24]df_key=pd.DataFrame({'key':sort_key})df=pd.DataFrame({'name':data,'ranking':ranking})

2. Do an outer join using pd.merge

--

--

Chi
Chi

Written by Chi

Books | Marketing | Data Viz | Analytics & Experimentation | Entrepreneurship 💡Founder of beautydupes.xyz | Blog: goodmarketing.club

No responses yet