Answers for "how to sort a dataframe using a list in python"

5

how to sort in pandas

// Single sort 
>>> df.sort_values(by=['col1'],ascending=False)
// ascending => [False(reverse order) & True(default)]
// Multiple Sort
>>> df.sort_values(by=['col1','col2'],ascending=[True,False])
// with apply() 
>>> df[['col1','col2']].apply(sorted,axis=1)
// axis = [1 & 0], 1 = 'columns', 0 = 'index'
Posted by: Guest on July-06-2020
1

Python sort dataframe by list

# sorts the dataframe so resulting order of ColumnName matches order in list
df = df.set_index(ColumnName).reindex(list_to_sort_by).reset_index()
Posted by: Guest on June-06-2021

Code answers related to "how to sort a dataframe using a list in python"

Python Answers by Framework

Browse Popular Code Answers by Language