Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polars: Create column with fixed value from variable

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

like image 517
rchitect-of-info Avatar asked Sep 12 '25 04:09

rchitect-of-info


2 Answers

Use polars.lit

import polars as pl

version = 6
df = df.with_columns(pl.lit(version).alias('VERSION'))

How to add new column to Latest (2024) Polars DataFrame:

In this example we are adding a uuid column to dataframe

import uuid
import polars as pl

# Generate a UUID
uuid = str(uuid.uuid4())

# Add a new column with the generated UUID to the DataFrame
df = df.with_columns(uuid=pl.lit(uuid))

Doc:

  • lit
  • with_columns()
like image 20
Abhi Avatar answered Sep 14 '25 16:09

Abhi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!