Your logic condition is wrong. IIUC, what you want is:

import pyspark.sql.functions as f

df.filter((f.col('d')<5))\
    .filter(
        ((f.col('col1') != f.col('col3')) | 
         (f.col('col2') != f.col('col4')) & (f.col('col1') == f.col('col3')))
    )\
    .show()

I broke the filter() step into 2 calls for readability, but you could equivalently do it in one line.

Output:

+----+----+----+----+---+
|col1|col2|col3|col4|  d|
+----+----+----+----+---+
|   A|  xx|   D|  vv|  4|
|   A|   x|   A|  xx|  3|
|   E| xxx|   B|  vv|  3|
|   F|xxxx|   F| vvv|  4|
|   G| xxx|   G|  xx|  4|
+----+----+----+----+---+
Answer from pault on Stack Overflow
Discussions

Filtering multiple conditions RDD
I'd make a single parsing function to do it in one pass: def get_month(text: str) -> Tuple[str, int]: for month in range(1,13): # Go through each month year_month = f'2020-{month:02}' # Make your string if year_month in text: # Check if it matches return (year_month, 1) # Couldn't find any month return ('0000-00', 1) a.map(get_month).filter(lambda x: x[0] != '0000-00') More on reddit.com
๐ŸŒ r/PySpark
7
1
March 31, 2021
Databricks Pyspark filter several columns with similar criteria
I am querying a table from the Databricks Catalog which I have to filter several columns with the same criteria. below is what I have created so far. I have 10 columns that I have filter with a set of criteria from (dx_list1) and another 10 that I have to filter with another set of criteria ... More on community.databricks.com
๐ŸŒ community.databricks.com
0
October 13, 2024
Panda - filter multiple columns by multiple values
You can use .isin(): RawData["Column3"].isin(["AAA", "BBB"]) More on reddit.com
๐ŸŒ r/learnpython
7
1
June 21, 2024
Filtering list of objects on multiple conditions
Hi all, interested in your approaches and suggestions to this problem. Let's say I have created a custom Player class that contains many attributesโ€ฆ More on reddit.com
๐ŸŒ r/learnpython
4
1
July 27, 2023
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ pyspark-filter-dataframe-based-on-multiple-conditions
Pyspark - Filter dataframe based on multiple conditions - GeeksforGeeks
November 28, 2022 - Example 2: Filter column with multiple conditions. ... # Using SQL col() function from pyspark.sql.functions import col dataframe.filter((col("college") == "DU") & (col("student_NAME") == "Amit")).show()
๐ŸŒ
Data Engineering Lab
py-spark-sql.com โ€บ pyspark โ€บ resources โ€บ filter-multiple-conditions
Filter Multiple Conditions in PySpark โ€” The Complete Beginner's G...
Learn Filter Multiple Conditions in PySpark โ€” The Complete Beginner's Guide in this PySpark resource with clear examples, data engineering context, and links into hands-on practice.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ pyspark โ€บ pyspark where() & filter() for efficient data filtering
PySpark where() & filter() for efficient data filtering - Spark By {Examples}
May 6, 2026 - In this PySpark article, you will learn how to apply a filter on DataFrame columns of string, arrays, and struct types by using single and multiple
๐ŸŒ
DataScience Made Simple
datasciencemadesimple.com โ€บ home โ€บ subset or filter data with multiple conditions in pyspark
Subset or Filter data with multiple conditions in pyspark - DataScience Made Simple
April 15, 2023 - Subset or filter data with multiple conditions in pyspark can be done using filter function() and col() function along with conditions inside the filter functions with either or / and operator
๐ŸŒ
Medium
medium.com โ€บ @uzzaman.ahmed โ€บ how-to-use-where-and-filter-in-a-dataframe-with-examples-bc2a9544ae3f
How to use `where()` and `filter()` in a DataFrame with Examples | by Ahmed Uz Zaman | Medium
January 31, 2023 - You can chain multiple conditions together using the & (and) or | (or) operators. ... For example, the following code filters a DataFrame named df to retain only rows where the column age is greater than 30 and the column gender is equal to ...
Find elsewhere
๐ŸŒ
Spark Code Hub
sparkcodehub.com โ€บ pyspark โ€บ dataframe โ€บ filter-rows-multiple-conditions
How to Filter Rows Based on Multiple Conditions in a PySpark DataFrame: The Ultimate Guide
The primary method for filtering rows in a PySpark DataFrame is the ... where()), which selects rows meeting specified conditions. To filter based on multiple conditions, combine boolean expressions using logical operators (
๐ŸŒ
Machine Learning Plus
machinelearningplus.com โ€บ blog โ€บ pyspark filter vs where โ€“ comprehensive guide filter rows from pyspark dataframe
PySpark Filter vs Where - Comprehensive Guide Filter Rows from PySpark DataFrame - machinelearningplus
April 16, 2023 - You can combine multiple filter conditions using the โ€˜&โ€™ (and), โ€˜|โ€™ (or), and โ€˜~โ€™ (not) operators. Make sure to use parentheses to separate different conditions, as it helps maintain the correct order of operations. Example: Filter rows with age greater than 25 and name not equal to โ€œDavidโ€ ... +---+-------+---+ | id| name|age| +---+-------+---+ | 1| Alice| 30| | 3|Charlie| 35| +---+-------+---+ we covered different ways to filter rows in PySpark ...
๐ŸŒ
PythonForBeginners
pythonforbeginners.com โ€บ home โ€บ pyspark filter rows in a dataframe by condition
PySpark Filter Rows in a DataFrame by Condition - PythonForBeginners.com
July 24, 2023 - Hence, the program runs into Py4JError. Instead of the filter method, you can also use sql WHERE clause to filter a pyspark dataframe by multiple conditions. For this, you can pass all the conditions in the WHERE clause and combine them using conditional operators.
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ python tutorial โ€บ pyspark filter
PySpark Filter | Functions of Filter in PySpark with Examples
April 19, 2023 - PySpark Filter condition is applied on Data Frame with several conditions that filter data based on Data, The condition can be over a single condition to multiple conditions using the SQL function.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
HKR Trainings
hkrtrainings.com โ€บ home โ€บ sample resumes โ€บ big data analytics โ€บ pyspark filter
โžค PySpark Filter | A Step by Step Guide on PySpark Filter
June 12, 2024 - In PySpark, an operation called filter() transformation helps filter the elements from PySpark RDD. Further, it returns an RDD with elements that pass the given conditions. There are certain methods to filter multiple columns in the PySpark DataFrame, such as:-
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ subset-or-filter-data-with-multiple-conditions-in-pyspark
Subset or Filter data with multiple conditions in PySpark - GeeksforGeeks
May 16, 2021 - # subset or filter the dataframe by # passing Multiple condition df = df.filter("Gender == 'Male' and Percentage>70") df.show()
๐ŸŒ
Reddit
reddit.com โ€บ r/pyspark โ€บ filtering multiple conditions rdd
r/PySpark on Reddit: Filtering multiple conditions RDD
March 31, 2021 -

Iโ€™m trying to sort some date data I have into months. They are stored as strings, not dates as I havenโ€™t found a way to do this using RDDs yet. I do not want to convert to a data frame. For example, I have:

Jan = a.filter(lambda x: โ€œ2020-01โ€ in x).map(lambda x: (โ€œ2020-01โ€, 1))

Feb = a.filter(lambda x: โ€œ2020-02โ€ in x).map(lambda x: (โ€œ2020-02โ€, 1))

March = a.filter(lambda x: โ€œ2020-03โ€ in x).map(lambda x: (โ€œ2020-03โ€, 1))

Etc for all the months. I then joined all these with a union so I could group them later. However this took a very long time because of so much happening. What would be a better way to filter these so that I could group them by month later?

๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ apache spark โ€บ spark dataframe where filter | multiple conditions
Spark DataFrame Where Filter | Multiple Conditions - Spark By {Examples}
May 6, 2026 - Spark filter() or where() function filters the rows from DataFrame or Dataset based on the given one or multiple conditions. You can use where() operator
๐ŸŒ
Idetail
idetail.ca โ€บ update โ€บ pyspark-filter-multiple-conditions
Idetail
September 25, 2024 - These methods allow you to specify conditions that rows must meet to be included in the result set. When dealing with multiple conditions, you can use logical operators such as AND, OR, and NOT to combine them.
๐ŸŒ
Databricks
community.databricks.com โ€บ databricks community โ€บ get started with databricks discussion โ€บ databricks pyspark filter several columns with similar criteria
Databricks Pyspark filter several columns with sim... - Databricks Community - 93658
October 13, 2024 - I am querying a table from the Databricks Catalog which I have to filter several columns with the same criteria. below is what I have created so far. I have 10 columns that I have filter with a set of criteria from (dx_list1) and another 10 that I have to filter with another set of criteria (dx_li...
๐ŸŒ
Kontext
kontext.tech โ€บ home โ€บ blogs โ€บ code snippets & tips โ€บ pyspark dataframe - filter records using where and filter function
PySpark DataFrame - Filter Records using where and filter Function - Kontext Labs
July 18, 2022 - We can use & or | to specify multiple conditions in one filter. The following script shows how to use filters. from pyspark.sql import SparkSession import pyspark.sql.functions as F appName = "PySpark DataFrame - where or filter" master = "local" ...