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()
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