Pular para o conteúdo principal

filtro (DataFrame)

Filtra as linhas usando a condição fornecida.

Sintaxe

filter(condition: Union[Column, str])

Parâmetros

Parâmetro

Tipo

Descrição

condition

Coluna ou str

Uma coluna do tipo booleano ou uma sequência de expressões SQL .

Devoluções

DataFrameUm novo DataFrame com linhas que satisfazem a condição.

Exemplos

Python
df = spark.createDataFrame([
(2, "Alice", "Math"), (5, "Bob", "Physics"), (7, "Charlie", "Chemistry")],
schema=["age", "name", "subject"])

df.filter(df.age > 3).show()
# +---+-------+---------+
# |age| name| subject|
# +---+-------+---------+
# | 5| Bob| Physics|
# | 7|Charlie|Chemistry|
# +---+-------+---------+

df.where(df.age == 2).show()
# +---+-----+-------+
# |age| name|subject|
# +---+-----+-------+
# | 2|Alice| Math|
# +---+-----+-------+

df.filter("age > 3").show()
# +---+-------+---------+
# |age| name| subject|
# +---+-------+---------+
# | 5| Bob| Physics|
# | 7|Charlie|Chemistry|
# +---+-------+---------+

df.filter((df.age > 3) & (df.subject == "Physics")).show()
# +---+----+-------+
# |age|name|subject|
# +---+----+-------+
# | 5| Bob|Physics|
# +---+----+-------+