Skip to main content

isin

Check if the column value is in a list of values.

Syntax

Python
isin(*cols)

Parameters

Parameter

Type

Description

cols

values

List of values to check against

Returns

Column (boolean)

Examples

Example 1: Filter rows with names in the specified values.

Python
df = spark.createDataFrame([(2, "Alice"), (5, "Bob"), (8, "Mike")], ["age", "name"])
df[df.name.isin("Bob", "Mike")].orderBy("age").show()
Output
# +---+----+
# |age|name|
# +---+----+
# | 5| Bob|
# | 8|Mike|
# +---+----+

Example 2: Filter rows with ages in the specified list.

Python
df[df.age.isin([1, 2, 3])].show()
Output
# +---+-----+
# |age| name|
# +---+-----+
# | 2|Alice|
# +---+-----+

Example 3: Filter rows with names not in the specified values.

Python
df[~df.name.isin("Alice", "Bob")].show()
Output
# +---+----+
# |age|name|
# +---+----+
# | 8|Mike|
# +---+----+

Example 4: Use a DataFrame as an IN subquery.

Python
df.where(df.age.isin(spark.range(6))).orderBy("age").show()
Output
# +---+-----+
# |age| name|
# +---+-----+
# | 2|Alice|
# | 5| Bob|
# +---+-----+