📎 Referral Code: —
🎁 Referral Earnings ₹0 →
📊 Dashboard Sign In
🎁 ₹0
Your Referral Earnings → Tap to view
Buy
🛒
Buy Course
Videos
📘
Basic Videos
🚀
Project Related Videos
🐍
Old Videos Style — will be removed soon
📚
Buy Book
After Course Completion
📒
My Interview Tracker — HR calls & follow-ups
📈
My Performance — Track your progress
🎯
Interview Prep — Resume & questions for a job
🏢
Company Interview Questions — with answers
🤖
Central Command — your HQ bot
💼
Jobs — AWS Data Engineer openings
Personal Training
🎬
Live Classes
🎯
My 1:1 Dashboard
🗺️
Course Roadmap
🎤
Mock Interview
🧑‍💼
HR Discussion
🗓️
Slot Booking
📅
My Booked Slots
Passive Income
🎁
Refer & Earn — ₹1,000 per referral
🤝
Browse Job Support
💰
Become a Supporter
🔗
Browse Referrals
📢
Post a Referral
🎓
Course Marketplace — Other Trainers
🧑‍🏫
Teach a Course
📊
My Courses & Earnings
🎬
Reels
AI Tools
🌐
Project Explanation Agent
👤
My Profile
🏢
Recruiter Board
🛟
Support Works

← DataZymes interview questions

🎬No video for this question yet. The written answer is below.

Write Filter for or condition

🏢 DataZymes 🏷️ Spark #4 of 4

📝 Answer

Interviewer:
Can you write a filter condition using OR in PySpark? Also explain how it works in a real-time scenario.
Candidate:
So basically, when I hear this question, I don’t just think about syntax… I think about how filtering impacts performance and data reduction. Let me explain step by step. In PySpark, we use the filter or where method to apply conditions. For OR conditions, we use the pipe symbol (|), not the Python “or” keyword. This is very important because using “or” will not work correctly with column objects. Let me show the code first.
from pyspark.sql import SparkSession

spark = SparkSession.builder.getOrCreate()

data = [
    (1, "Hyderabad", 5000),
    (2, "Bangalore", 7000),
    (3, "Chennai", 4000),
    (4, "Hyderabad", 3000)
]

columns = ["id", "city", "salary"]

df = spark.createDataFrame(data, columns)

# OR condition filter
filtered_df = df.filter((df.city == "Hyderabad") | (df.salary > 6000))

filtered_df.show()
So basically, what is happening here is… We are filtering records where either the city is Hyderabad OR salary is greater than 6000. Now let me connect this to a real-time scenario. In one of my projects, I had to filter data based on multiple conditions like region or revenue threshold. For example, I needed to process records where region is “US” OR revenue is above a certain limit. Using OR conditions helped me combine multiple business rules in a single filter. One important thing I learned is always use parentheses properly, because logical conditions can behave unexpectedly if not grouped correctly. Also, filtering early in the pipeline helps reduce data size and improves performance. So basically, OR condition in PySpark is implemented using “|” operator, and it is commonly used for combining multiple filtering conditions.
Interviewer (Follow-up):
Good. What will happen if you use Python 'or' instead of '|' in PySpark?