I have a Polars dataframe and want to sort the columns alphabetically without access to a dataframe variable.
In this example I want the column order to be ['a','b']:
import polars as pl
df = pl.DataFrame({'b':[0,1],'a':[2,3]})
If I have the dataframe variable I can do:
df = df.select(sorted(df.columns))
But I want to do it within a query.
This is particularly relevant after doing a suffix
to get the columns with the same root column together
(
df
.with_columns(
pl.all().rank().suffix("_rank")
)
)