20

Being a new user to polars coming from pandas, I have searched polars GitHub pages, user guide, stackoverflow and discord channel on how to add a new column to a polars dataframe. I have only found polars examples on how to add new columns based on existing ones.

How should the following pandas example be converted to polars syntax?

import polars as pl

df = pl.DataFrame({'existing_column': [1, 2, 3]})

df_pd = df.to_pandas()
df_pd['new_column'] = 'some text'

With expected content of final dataframe:

existing_column new_column
1 some_text
2 some_text
3 some_text
0

2 Answers 2

34

you can use with_columns and pl.lit to add a new column with Polars that is a constant and not based on existing columns.

You can use it for both text and numbers.

Here is an example:

df.with_columns(
    new_column = pl.lit('some_text')
)

The advantage of Polars is that all new columns added and all columns transformed within the same with_columns will be calculated in parallel.

Sign up to request clarification or add additional context in comments.

2 Comments

Just a note after a good 20 min of time wasting, if you have a column name you can just do: df.with_columns(**{col_name_var: pl.lit('some_text')}) Which will be translated to df.with_columns(ACTUALVALUEOFcol_name_var = pl.lit('some_text'))
@E.Serra To any future reader: For that there existspl.Expr.alias.
11

Polars provides the user with pl.lit, which can be used within pl.DataFrame.with_columns to create a new column with a literal value (string, int, float).

df.with_columns(
    pl.lit("some_text").alias("new_column")
)

pl.Expr.alias is used to assign a name to the new column.

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.