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?

Beginnerโฑ 2 Minutesโญ 200 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