rchitect-of-info
rchitect-of-info

Reputation: 1586

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

Upvotes: 22

Views: 21881

Answers (2)

user18263465
user18263465

Reputation:

Use polars.lit

import polars as pl

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

Upvotes: 42

Abhi
Abhi

Reputation: 1005

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:

Upvotes: 0

Related Questions