Get startedGet started for free

Creating conditional expressions

1. Creating conditional expressions

Now let's add smarter categorization to our app using conditional expressions.

2. Categorizing by rating quality

Our app users want to quickly identify which restaurants are highly rated. We'll categorize restaurants as "Highly Rated" if they have a rating of 5, otherwise "Needs Improvement". To do this, we use the Polars equivalent of an if-else condition.

3. Categorizing by rating quality

We start by using with_columns to add a new column

4. Categorizing by rating quality

and we create an if statement using pl.when

5. Categorizing by rating quality

Inside this, we use a predicate expression to capture when the rating equals 5.

6. Categorizing by rating quality

We tell Polars what the value should be when the rating is 5 with the then expression. Inside, we pass pl.lit("Highly Rated"). We must use pl.lit here, or Polars looks for a column called "Highly Rated".

7. Categorizing by rating quality

We follow with the .otherwise() expression to specify the value when the condition is false - "Needs Improvement".

8. Categorizing by rating quality

Finally, we name the column quality.

9. Categorizing by rating quality

This gives us a new column where businesses with ratings of 5 are marked as "Highly Rated" while those with lower ratings are marked as "Needs Improvement".

10. Categorizing by venue size

Next, let's classify venues by size. We'll categorize them into three groups: "Large" for capacity over 100, "Medium" for 20 to 100, and "Small" for under 20. To do this, we need to apply an if-elif-else condition.

11. Categorizing by venue size

We start by using with_columns to add a new column

12. Categorizing by venue size

and use pl.when to first check if capacity is greater than 100

13. Categorizing by venue size

Followed by .then() to return "Large" when the first condition is true.

14. Categorizing by venue size

Then we chain another .when to check for medium-sized venues with capacity greater than or equal to 20.

15. Categorizing by venue size

And set the value to Medium in this case

16. Categorizing by venue size

Finally, we use .otherwise to return "Small" for all other cases.

17. Categorizing by venue size

And name the column venue_size. This gives us a new column with three categories. The Queens Head with capacity 187 is "Large", 7burgers with capacity 55 is "Medium", and the Costa Coffee takeaway with capacity 0 is "Small". In this case, we used two pairs of when-then expressions, but you can chain as many as you need!

18. Standardizing business types

Our market research shows that categorizing businesses as restaurants or cafés doesn't always align with users' perceptions. To simplify things, we want to have just a single "restaurant" category for our app by replacing café with restaurant.

19. Standardizing business types

To do this, we start by creating an expression on the type column

20. Standardizing business types

And use the .replace expression. The replace method takes the current value and the new value as arguments.

21. Standardizing business types

Which in our case means replacing café with restaurant. The third row now shows "restaurant" instead of "café" and we have a more consistent categorization.

22. Let's practice!

We've built rating labels, venue size categories, and standardized types. Now it's your turn to practice conditional expressions!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.