Skip to content

Instantly share code, notes, and snippets.

@seanpue
Created August 6, 2015 01:01
Show Gist options
  • Save seanpue/e1cb846f676194ae77eb to your computer and use it in GitHub Desktop.
Save seanpue/e1cb846f676194ae77eb to your computer and use it in GitHub Desktop.
Sort pandas dataframe using icu locale
import PyICU
# below from http://stackoverflow.com/questions/13838405/custom-sorting-in-pandas-dataframe
df = token_count
locale = 'UR.UTF-8'
collator = icu.Collator.createInstance(icu.Locale(locale))
def sort_pd(key=None,reverse=False,cmp=None):
def sorter(series):
series_list = list(series)
return [series_list.index(i)
for i in sorted(series_list,key=key,reverse=reverse,cmp=cmp)]
return sorter
sort_by_custom_dict = sort_pd(key=collator.getSortKey)
df.iloc[sort_by_custom_dict(df.index)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment