Open In App

Add New Columns to Polars DataFrame

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Polars is a fast DataFrame library implemented in Rust and designed to process large datasets efficiently. It is gaining popularity as an alternative to pandas, especially when working with large datasets or needing higher performance. One common task when working with DataFrames is adding new columns, which can be done easily in Polars.

In this article, we'll explore different methods to add a new column to a Polars DataFrame.

Install Polars

We first make sure that we have Polars installed in our system.

pip install polars

Create and Add a Column to Polars DataFrame

Let's start by creating a simple Polars DataFrame to work with. Here we will create a DataFrame with two columns name and age.

Python
import polars as pl

# Sample DataFrame
df = pl.DataFrame({
    "name": ["Alice", "Bob", "Charlie"],
    "age": [25, 30, 35]
})

print(df)

Output

Screenshot-2024-08-29-205406
Polasrs dataframe

Method 1: Adding a New Column with a Constant Value

To add a new column with the same value for all rows, we can use the with_columns method.

Here, we will add a new column named 'city' with the value "New York" for all rows.

Python
# Add a new column 'city' with the same value for all rows
df = df.with_columns(pl.lit("New York").alias("city"))

print(df)
Screenshot-2024-08-29-205412
Add a new column with with constant value

Method 2: Adding a New Column Based on Existing Columns

We can also add a new column that is computed from existing columns. For example, let's add a column called age_in_5_years that adds 5 years to the age column.

Python
# Add a new column 'age_in_5_years' based on the 'age' column
df = df.with_columns((pl.col("age") + 5).alias("age_in_5_years"))

print(df)
Screenshot-2024-08-29-205418
Add columns uising existing ones.

Method 3: Adding a New Column from a List

If we want to add a new column with values from a list, we can do so by passing the list directly to pl.Series. He we will add a new column named salary with values [50000, 60000, 70000] for each corresponding row.

Python
# Add a new column 'salary' with values from a list
df = df.with_columns(pl.Series("salary", [50000, 60000, 70000]))

print(df)
Screenshot-2024-08-29-205424
Adding a new column from list

Method 4: Adding a New Column Using a Function

Sometimes, we might want to create a new column by applying a function to existing columns. This can be done using the apply method.

For example, let's create a new column is_adult that returns True if age is greater than or equal to 18:

Python
# Add a new column 'is_adult' using a function
df = df.with_columns((pl.col("age") >= 18).alias("is_adult"))

print(df)
Screenshot-2024-08-29-205430
Add a new column using function

Method 5: Adding Multiple Columns at Once

We can also add multiple columns at the same time using the with_columns method:

Python
# Add multiple columns
df = df.with_columns([
    (pl.col("age") * 2).alias("double_age"),
    pl.lit("Unknown").alias("country")
])

print(df)
Screenshot-2024-08-29-205436
Add multiple columns at a time

Conclusion

Adding new columns to a Polars DataFrame is simple and flexible, allowing us to create derived data, insert constant values, or even apply complex functions. Polars' syntax is intuitive, making it easy to perform operations similar to what you would do in pandas but with the added benefit of Polars' performance.


Article Tags :
Practice Tags :

Similar Reads