In the realm of big data processing, PySpark offers a robust and scalable solution, enabling data scientists and engineers to handle massive datasets efficiently. One of the powerful features that PySpark provides is the `when` function, allowing conditional transformations on DataFrame or Series columns. When working with multiple conditions, understanding how to use `when` function effectively becomes crucial.

Before diving into multiple conditions, let's quickly understand the basic usage of the `when` function. The `when` function takes two arguments: a condition and the value to return when the condition is true. If the condition is false, it returns `null` or `None`.

Using `when` Function with Multiple Conditions
The `when` function can be used with multiple conditions by combining them with logical operators (`&&` for AND, `||` for OR) in PySpark. However, writing multiple conditions directly can become cumbersome and less readable. PySpark provides a more readable and efficient way to handle multiple conditions using `ovies` and `otherwise`.

Let's first understand the `ovies` function, which is used to accumulate a list of conditions. When using `ovies`, PySpark returns a `When` object, which can be used to set a value when the condition is true. This `When` object can be `ovies` again to add more conditions, making it highly readable when dealing with multiple conditions.
Using `ovies` to Handle Multiple Conditions

Suppose you have a DataFrame `df` and you want to create a new column `status` based on multiple conditions on `age` and `salary` columns. You can do this using the `ovies` function as follows:
```python from pyspark.sql.functions import when, col df = df.withColumn( "status", when((col("age") > 30) & (col("salary") > 50000), "Senior") .otherwise("Junior") ) ```
Here, we create a `When` object using `when` function with the condition `(col("age") > 30) & (col("salary") > 50000)`, and then use `otherwise` to set the value when the condition is false. The result is a new column `status` that contains "Senior" if both age is greater than 30 and salary is greater than 50000, and "Junior" otherwise.
You can `ovies` additional conditions to this `When` object to create more complex transformations. For example:

```python df = df.withColumn( "status", when((col("age") > 30) & (col("salary") > 50000), "Senior") .when((col("age") > 20) & (col("salary") <= 50000), "Mid-level") .otherwise("Junior") ) ```
In this example, we create three categories based on age and salary: "Senior", "Mid-level", and "Junior".
Using Case When with Multiple Conditions
Another way to handle multiple conditions in PySpark is to use the `case when` SQL expression. `case when` is a SQL construct that allows you to evaluate one or more conditions and returns a value based on the first condition that evaluates to true.

Here's how you can use `case when` to create the same `status` column as above:
```python from pyspark.sql.functions import col, Expression df = df.withColumn( "status", (col("age") > 30) & (col("salary") > 50000).cast("string") == "Senior" .otherwise((col("age") > 20) & (col("salary") <= 50000).cast("string") == "Mid-level") .otherwise("Junior") ) ```
While using `ovies` provides a more readable and Pythonic way to handle multiple conditions, `case when` can be more efficient for complex conditions and is closer to the SQL syntax, making it easier to understand for SQL users.









Selecting Top N Records with Condition
Another use case for multiple conditions in PySpark is when selecting top N records based on certain criteria. PySpark provides the `limit` function to select a certain number of records, but it does not provide a direct way to select top N records based on a condition.
To achieve this, you can use a combination of `where`, `orderBy`, and `limit` functions. Here's an example:
Using `where`, `orderBy`, and `limit` to Select Top N Records with Condition
Suppose you have a DataFrame `df` with `name` and `salary` columns, and you want to select the top 5 employees with the highest salary who are older than 25. You can do this as follows:
```python top_employees = df.where(col("age") > 25).orderBy(col("salary").desc()).limit(5) ```
Here, we first filter the DataFrame to include only employees older than 25 using the `where` function. Then, we order the DataFrame by salary in descending order using the `orderBy` function. Finally, we select the top 5 records using the `limit` function.
The result `top_employees` is a DataFrame containing the top 5 employees with the highest salary who are older than 25.
Remember, in the case of ties, PySpark uses a stable sort, which means that the order of records with the same salary will remain the same as in the original DataFrame.
In the world of big data processing, mastering PySpark's `when` function and its variants for handling multiple conditions is crucial for extracting meaningful insights from complex datasets.