Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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

pandas sort by list

# Convert Tm-column to category and in set the sorter as categories hierarchy
# Youc could also do both lines in one just appending the cat.set_categories()
df.Tm = df.Tm.astype("category")
df.Tm.cat.set_categories(sorter, inplace=True)

print(df.Tm)
Out[48]: 
0    CHH
1    VAN
2    TOT
3    OKC
4    DAL
Name: Tm, dtype: category
Categories (38, object): [TOT < ATL < BOS < BRK ... UTA < VAN < WAS < WSB]

df.sort_values(["Tm"])  ## 'sort' changed to 'sort_values'
Out[49]: 
   Age   G           Player   Tm  Year     id
2   22  60      Ratko Varda  TOT  2001  13950
0   27   6    Cedric Hunter  CHH  1991   2967
4   31  81  Adrian Caldwell  DAL  1997   6169
3   34  52       Ryan Bowen  OKC  2009   6141
1   25   7    Maurice Baker  VAN  2004   5335
Comment

PREVIOUS NEXT
Code Example
Python :: create text in python if not exists 
Python :: calculate euclidian distance python 
Python :: formula for compounding interest in python 
Python :: kivymd simple button 
Python :: python year month from date 
Python :: how to join a string by new line out of a list python 
Python :: how to minimize command console python 
Python :: resize image array python 
Python :: csrf token exempt django 
Python :: pyspark session 
Python :: Tensorflow not installing error 
Python :: string array to float array python 
Python :: PySpark get columns with null or missing values 
Python :: flask getting started 
Python :: how to see the functions of a library in python 
Python :: append dataframe to another dataframe 
Python :: tensorflow plot model 
Python :: get all classes from css file using python 
Python :: today date python 
Python :: how to use random in python 
Python :: bs4 from url 
Python :: classification report value extration 
Python :: how to get the user ip in djagno 
Python :: pyspark import stringtype 
Python :: extract name organization using nltk 
Python :: flatten a list of list python 
Python :: remove item from list while looping 
Python :: factorial python for loop 
Python :: detect stop codon 
Python :: watch dogs 3 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =