pandas sort by list

Solutions on MaxInterview for pandas sort by list by the best coders in the world

showing results for - "pandas sort by list"
Eleonora
11 Apr 2017
1# sorts the dataframe so resulting order of ColumnName matches order in list
2df = df.set_index(ColumnName).reindex(list_to_sort_by).reset_index()
Rafael
28 Oct 2018
1# Convert Tm-column to category and in set the sorter as categories hierarchy
2# Youc could also do both lines in one just appending the cat.set_categories()
3df.Tm = df.Tm.astype("category")
4df.Tm.cat.set_categories(sorter, inplace=True)
5
6print(df.Tm)
7Out[48]: 
80    CHH
91    VAN
102    TOT
113    OKC
124    DAL
13Name: Tm, dtype: category
14Categories (38, object): [TOT < ATL < BOS < BRK ... UTA < VAN < WAS < WSB]
15
16df.sort_values(["Tm"])  ## 'sort' changed to 'sort_values'
17Out[49]: 
18   Age   G           Player   Tm  Year     id
192   22  60      Ratko Varda  TOT  2001  13950
200   27   6    Cedric Hunter  CHH  1991   2967
214   31  81  Adrian Caldwell  DAL  1997   6169
223   34  52       Ryan Bowen  OKC  2009   6141
231   25   7    Maurice Baker  VAN  2004   5335
24
queries leading to this page
sort dataframe by list pandaspandas sort by listpandas sort dataframe by listsort list in python pandas columnsort by pandassort pandas listhow to use sort by list in pandashow to sort dataframe as per list sequence order list pandaspandas sort df by given listpandas sort listpython dataframe order according to listpandas sort columns by a listorder dataframe by listpandas sort df with listlist sort pandassort pandas dataframe based on listorder by list pandassort a pandas listsort list with pandaspandas dataframe sort columns by listsort in python pandaspandas sort a listpandas dataframe sort by listpandas column sort by listsort dataframe by listsort df on listpandas order rows alphabeticallylist df sortdataframe sorting using listpandas sort by listpandas how to sort a listpandas sort a list columnpython sort dataframe given listpandas sort rows into new listshow to sort a dataframe using a list in pythonpandas sort df specified by listpandas sort byonly read section of order list pandassort a dataframe based on a listhow to sort dataframe in pandas using a listpython df sortdataframe sort rows by listsort dataframe as listpandas order by listorder by pandassort columns by list order pandassort a list in pandas dataframe columnpython pandas sort by listpanda sort values in listsort by pandaspandas sort values by listsort list pandasi have a list of data and dataframe want to sort dataframe as per list sequencesort dataframe based on a given listpandas sort columns based on listpandas sort python order dataframe column by listprint sort list of column pythonpandas sort by list