Menu
Stuff by Yuki
  • Home
  • Python
  • Power BI
  • Tableau
  • Community
    • Makeover Monday
    • Workout Wednesday
  • About
  • Contact
Stuff by Yuki

Conditional Logic or If-Else in Polars

Posted on May 23, 2023May 23, 2023

One of the most common patterns in data is the need for conditional logic or if-else statements. You may want to assign a specific value when a certain condition is met. This is easily done in Pandas, using numpy.where() or pandas.where(). But how about Polars? The short answer is yes, you can do the same in Polars. You’d use polars.when().

You can find my Python script used in this post in my GitHub repo.

Implement Conditional Logic in Polars

As mentioned above, you use polars.when() to implement conditional logic or if-else statements in Polars.

Let’s say I have a dataframe that contains numbers from 1 to 5.

Copy Copied Use a different Browser

import polars as pl

df = pl.LazyFrame(
    {'Numbers': [1,2,3,4,5]}
)

print(df.collect().head())
'''
output:
shape: (5, 1)
┌─────────┐
│ Numbers │
│ ---     │
│ i64     │
╞═════════╡
│ 1       │
│ 2       │
│ 3       │
│ 4       │
│ 5       │
└─────────┘
'''

Now I add the portion that adds a new column assigning values depending on the value of “Number” column. Use .when() to define your condition and use .otherwise() to assigning “else” values.

Copy Copied Use a different Browser

df = (
    df
    .with_columns(
        pl.when(pl.col('Numbers')==1)
        .then('Best')
        .when(pl.col('Numbers')==2)
        .then('Second Best')
        .otherwise('Not Good')
        .alias('Rank')
    )
    
)

print(df.collect().head())
'''
output:
shape: (5, 2)
┌─────────┬─────────────┐
│ Numbers ┆ Rank        │
│ ---     ┆ ---         │
│ i64     ┆ str         │
╞═════════╪═════════════╡
│ 1       ┆ Best        │
│ 2       ┆ Second Best │
│ 3       ┆ Not Good    │
│ 4       ┆ Not Good    │
│ 5       ┆ Not Good    │
└─────────┴─────────────┘
'''

Summary

Your code can be a little lengthy when using polars.when(), but it’s much more readable than pandas.where() in my opinion as polars.when() is more descriptive and easy to read the coder’s intention.

GitHub repo

Reference

https://pola-rs.github.io/polars/py-polars/html/reference/expressions/api/polars.when.html

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How to Convert String to Date or Datetime in Polars
  • Aggregations Over Multiple Columns in Polars
  • DuckDB with Polars, Pandas, and Arrow
  • Read from and Write to Amazon S3 in Polars
  • Handling Missing Values in Polars

Popular Posts

  • A Running Total Calculation with Quick Measure in Power BI
  • How To Copy And Paste Report Page in Power BI
  • How to Fill Dates Between Start Date and End Date in Power BI (Power Query)
  • Year-Over-Year Calculation: Time Intelligence in Power BI
  • Network Visualizations in Python

connect with me

  • LinkedIn
  • Twitter
  • Github
©2023 Stuff by Yuki | Powered by SuperbThemes & WordPress