Condividi tramite


filter (DataFrame)

Filtra le righe usando la condizione specificata.

Sintassi

filter(condition: Union[Column, str])

Parametri

Parametro Tipo Descrizione
condition Colonna o str Colonna di tipo BooleanType o stringa di espressioni SQL.

Restituzioni

DataFrame: nuovo dataframe con righe che soddisfano la condizione.

Examples

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|
# +---+----+-------+