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 │ └─────┴─────┴───────┘