Home

Published

- 1 min read

Python sort dataframe by list

img of Python sort dataframe by list

The solution for this is noted below

Python sort dataframe by list

Solution

   # sorts the dataframe so resulting order of ColumnName matches order in list
df = df.set_index(ColumnName).reindex(list_to_sort_by).reset_index()

Try other methods by searching on the site. That is if this doesn’t work