0

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")
    )
)
braaannigan
  • 594
  • 4
  • 12

1 Answers1

2

This can be done with the pipe method:

(
    df
    .with_columns(
        pl.all().rank().suffix("_rank")
    )
    .pipe(
        lambda tempDf: tempDf.select( sorted(tempDf.columns) )
    )
)
braaannigan
  • 594
  • 4
  • 12