Add new column to polars DataFrame

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 pandas as pd
df = pd.DataFrame({'existing_column': [1, 2, 3]})
df['new_column'] = "some text"

With expected content of final dataframe:

existing_columnnew_column
1some_text
2some_text
3some_text

1 Answer

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.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like