Search code examples
python-polars

How to make a with_columns become the first column w/o selecting all columns


Let's say I have a simple df

df=pl.DataFrame(
{
    "date":["2022-01-01", "2022-01-02"],
    "hroff":[5,2],
    "minoff":[1,2]
}).with_columns(pl.col('date').str.to_date())

If I want to add a column I can do df=df.with_columns(pl.lit('abc').alias('newcolumn'))

but if I want that new column to be first is there a direct way to do that other than having to add .select('newcolumn','date','hroff','minoff')?


Solution

  • df.select(
        pl.lit('abc').alias('newcolumn'),
        pl.all()
    )