Reputation: 1586
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
Reputation:
Use polars.lit
import polars as pl
version = 6
df = df.with_columns(pl.lit(version).alias('VERSION'))
Upvotes: 42
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