pythonpython-polarspolars

Sort a polars dataframe based on an external list


Morning,

I'm not sure if this can be achieved..

Let's say i have a polars dataframe with cols a, b (whatever).

df = pl.DataFrame({"a":[1,2,3,4,5],"b":['x','y','z','p','f']})

And a list.. l = [1,3,5,2,4]; is it possible to sort the dataframe (using column "a") using the list l as the sorting order?

Thanks in advance!


Solution

  • You can use an Enum to sort with a custom order, however as Enum only works with strings, you first need to temporarily convert to string:

    df.sort(by=pl.col('a').cast(pl.String).cast(pl.Enum(list(map(str, l)))))
    

    Output:

    ┌─────┬─────┐
    │ a   ┆ b   │
    │ --- ┆ --- │
    │ i64 ┆ str │
    ╞═════╪═════╡
    │ 1   ┆ x   │
    │ 3   ┆ z   │
    │ 5   ┆ f   │
    │ 2   ┆ y   │
    │ 4   ┆ p   │
    └─────┴─────┘