What would be the most idiomatic (and efficient) way to add a column in front of a polars data frame? Same thing like .with_column but add it at index 0?

1 Answer

You can select in the order you want your new DataFrame.

df = pl.DataFrame({ "a": [1, 2, 3], "b": [True, None, False] }) df.select([ pl.lit("foo").alias("z"), pl.all() ]) 
shape: (3, 3) ┌─────┬─────┬───────┐ │ z ┆ a ┆ b │ │ --- ┆ --- ┆ --- │ │ str ┆ i64 ┆ bool │ ╞═════╪═════╪═══════╡ │ foo ┆ 1 ┆ true │ ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌┤ │ foo ┆ 2 ┆ null │ ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌┤ │ foo ┆ 3 ┆ false │ └─────┴─────┴───────┘ 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy