serhii.net

In the middle of the desert you can say anything you want

12 Oct 2023

Custom ordering-sorting in pandas

To sort string values in a dataframe in a specific (non-alph.) order, one option is to use categorical variables1:

COLLECTIONS_ORDER = [
    "Final terms",
    "Base prospects",
    "Annual reports",
    "IFRS",
    "Law",
    "Brochures & Handouts",
    "BBK monthly reports",
]

df[KCOL] = pd.Categorical(df[KCOL], categories=COLLECTIONS_ORDER, ordered=True)

df.sort_values(KCOL, ascending=True)

Neat when doing graphs or summary/aggregated tables!

Nel mezzo del deserto posso dire tutto quello che voglio.