15

I have scrubbed the polars docs and cannot see an example of creating a column with a fixed value from a variable. Here is what works in pandas:

df['VERSION'] = version

Thx

rchitect-of-info
  • 1,150
  • 1
  • 11
  • 23

1 Answers1

32

Use polars.lit

import polars as pl

version = 6
df = df.with_columns(pl.lit(version).alias('VERSION'))
Björn
  • 1,610
  • 2
  • 17
  • 37
  • 1
    Here's some documentation on the `lit` expression: https://pola-rs.github.io/polars/py-polars/html/reference/api/polars.lit.html –  Mar 03 '22 at 16:55
  • Thanks @cbilot for the quick response. Trying to migrate all of pandas code for extract, transform and load into database to polars. – rchitect-of-info Mar 03 '22 at 17:37
  • 2
    `with_columns()` is now the correct method, `with_column` will throw an error. – abk Apr 14 '23 at 01:14