Polars List Comprehensions: The .list.eval() Method

3D visualization of a tiny robotic arm working inside a single data cell containing a list of items, representing Polars list.eval.

What if you have a column that contains lists, and you want to perform an operation on every item inside every list? In these situations, Polars list.eval can help you apply functions efficiently to list elements within your data.

Example: You have a list of sales, and you want to add a 10% tax to each one. [100, 200] -> [110, 220]

In Pandas, you’d use a slow .apply(). In Polars, you use the high-speed .list.eval() expression.

The list.eval() Method

It lets you run a Polars Expression on the contents of a list. Inside the expression, you use pl.element() to refer to the items.

import polars as pl
df = pl.DataFrame({
    "order_id": [1, 2],
    "sales": [[100, 200], [30, 40, 50]]
})
print(df)
shape: (2, 2)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ order_id โ”† sales         โ”‚
โ”‚ ---      โ”† ---           โ”‚
โ”‚ i64      โ”† list[i64]     โ”‚
โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚ 1        โ”† [100, 200]    โ”‚
โ”‚ 2        โ”† [30, 40, 50]  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Let’s add 10% tax to every sale in every list.

df_taxed = df.with_columns(
    pl.col("sales")
      .list.eval(
          pl.element() * 1.1 # 'pl.element()' refers to the items 100, 200, etc.
      )
      .alias("sales_with_tax")
)
print(df_taxed)

Output:

shape: (2, 3)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ order_id โ”† sales         โ”† sales_with_tax           โ”‚
โ”‚ ---      โ”† ---           โ”† ---                      โ”‚
โ”‚ i64      โ”† list[i64]     โ”† list[f64]                โ”‚
โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚ 1        โ”† [100, 200]    โ”† [110.0, 220.0]           โ”‚
โ”‚ 2        โ”† [30, 40, 50]  โ”† [33.0, 44.0, 55.0]       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

This is a powerful feature for nested data (like from JSON) and runs much faster than a standard Python for loop.


Key Takeaways

  • You can perform operations on every item inside lists using Polars instead of slow Pandas methods.
  • The Polars list.eval() method allows you to run expressions on list contents efficiently.
  • Using pl.element() in the expression lets you reference individual items in the list.
  • This method adds a 10% tax to every sale in each list quickly and effectively.
  • Polars list.eval is particularly powerful for handling nested data, like data from JSON.

Similar Posts

Leave a Reply