25

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

0

2 Answers 2

44

Use polars.lit

import polars as pl

version = 6
df = df.with_columns(pl.lit(version).alias('VERSION'))
Sign up to request clarification or add additional context in comments.

3 Comments

Here's some documentation on the lit expression: pola-rs.github.io/polars/py-polars/html/reference/api/…
Thanks @cbilot for the quick response. Trying to migrate all of pandas code for extract, transform and load into database to polars.
with_columns() is now the correct method, with_column will throw an error.
2

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:

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.