Skip to main content
PYSPARK • LESSON 38

Logical Inversion with NOT (~)

How do we exclude records matching specific country criteria using the bitwise NOT operator?

Beginner2 Minutes200 XP
🤔 THE QUESTION

How do we exclude records matching specific country criteria using the bitwise NOT operator?

💡 WHAT IS IT?

The tilde (~) operator inverts a boolean Column condition, keeping only records where the condition is False.

🎯 WHAT IS IT USED FOR?

International expansion analytics, excluding internal test accounts, and blacklisting fraudulent IPs.

💻 EXAMPLE
from pyspark.sql.functions import col

df.filter(~col("country").isin("US", "CA")).show()

🎯 Mission Objectives

Practice typing production-grade PySpark code for Logical Inversion with NOT (~).

  • Use ~ operator for negation
  • Invert isin() set condition
  • Filter international records