Open In App

How to Add a Column with Numerical Value in Polars

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

Polars is a high-performance DataFrame library written in Rust with Python bindings that offers a fast and efficient way to handle large datasets. In this article, we'll discuss how to add a column with numerical values to a Polars DataFrame, which is similar to operations in pandas but optimized for speed.

What is Polars in Python?

Polars is a DataFrame library designed to be fast and memory-efficient. It's gaining popularity for handling large datasets efficiently. One of the key tasks when working with time-series data or data containing timestamps is converting string-based date information into the Date or Datetime type. Polars provide built-in functionality to handle this with ease.

Polars can be installed using a simple pip command.

pip install polars

Once polars is installed we can verify it by writing a simple Python Script.

Python
# import module
import polars as pl

# print version
pl.show_versions()

Output:

Polars Installation
Polars Installation

Loading Data into Polars DataFrame

Before converting string columns to dates or datetimes, it is essential to load the data into a Polars DataFrame. Polars supports reading various file formats, including CSV, Parquet, and more.

Let us see step by step how we can load the data into Polars and remove duplicate values using the unique() function.

Import Polars

Once the Polars library is installed on your system, you can import it an use it in your programs.

import polars as pl

Load Data

Next step is to load the data into the Polars DataFrame using the DataFrame() function.

data = {
"name": ["Alice", "Bob", "Charlie"],
"age": [25, 30, 35]
}

Converting data to DataFrame

To add the data to a dataframe, the DataFrame() function is used and pass the data dictionary as a parameter.

df = pl.DataFrame(data)
print(df)

Here is a complete code for loading and printing the data in the Polars DataFrame.

Python
# import polars module
import polars as pl

# Sample Data
data = {
    "name": ["Alice", "Bob", "Charlie"],
    "age": [25, 30, 35]
}

# Create a Polars DataFrame
df = pl.DataFrame(data)

# Display the DataFrame
print(df)

Output:

Loading Data into Polars Dataframe
Loading Data into Polars Dataframe

Adding a Column with a Numerical Value

To add a new column with numerical values to an existing Polars DataFrame, we can use the with_columns() method. Here are different ways to do it:

1. Adding a Column with a Constant Numerical Value

If you want to add a column where every row contains the same numerical value, here’s how to do it:

Python
# import polars module
import polars as pl

# Sample Data
data = {
    "name": ["Alice", "Bob", "Charlie"],
    "age": [25, 30, 35]
}

# Create a Polars DataFrame
df = pl.DataFrame(data)

# adding a colums with a constant value
df = df.with_columns(pl.lit(50000).alias("salary"))

# Display the DataFrame
print(df)

Output:

Adding a Column with a Constant Value
Adding a Column with a Constant Value

2. Adding a Column with a List of Numerical Values

We can also add a column that contains different numerical values for each row by passing a list:

Python
# import polars module
import polars as pl

# Sample Data
data = {
    "name": ["Alice", "Bob", "Charlie"],
    "age": [25, 30, 35]
}

# Create a Polars DataFrame
df = pl.DataFrame(data)

# Adding a column with different values
df = df.with_columns(pl.Series("score", [85, 90, 95]))

# Display the DataFrame
print(df)
Adding a Column with a List of Values
Adding a Column with a List of Values

3. Adding a Column Based on an Existing Column

We can also create a new column based on calculations involving existing columns. For example, adding a bonus column where each value is 10% of the salary:

Python
# import polars module
import polars as pl

# Sample Data
data = {
    "name": ["Alice", "Bob", "Charlie"],
    "age": [25, 30, 35],
    "salary": [50000, 45000, 48000]
}

# Create a Polars DataFrame
df = pl.DataFrame(data)

# adding a column based on existing column
df = df.with_columns((df["salary"] * 0.1).alias("bonus"))

# Display the DataFrame
print(df)

Output:

Adding a Column based on Existing Column
Adding a Column based on Existing Column

4. Adding Multiple Columns at Once

If we want to add multiple columns at the same time, we can use the with_columns method:

Python
# import polars module
import polars as pl

# Sample Data
data = {
    "name": ["Alice", "Bob", "Charlie"],
    "age": [25, 30, 35],
    "salary": [50000, 45000, 48000]
}

# Create a Polars DataFrame
df = pl.DataFrame(data)

# adding multiple columns at once
df = df.with_columns([
    pl.lit(1).alias("department_id"),
    (df["salary"] * 0.15).alias("tax")
])

# Display the DataFrame
print(df)

Output:

Adding Multiple Columns at Once
Adding Multiple Columns at Once

Conclusion

Adding a column with numerical values to a Polars DataFrame is straightforward using methods like with_column and with_columns. We can add columns with constant values, lists, or even values derived from existing columns. Polars makes these operations highly efficient, allowing you to process large datasets quickly and effectively.


Article Tags :
Practice Tags :

Similar Reads