allisonwang-db commented on code in PR #42708:
URL: https://github.com/apache/spark/pull/42708#discussion_r1309192399
##########
python/pyspark/sql/dataframe.py:
##########
@@ -3355,48 +3355,131 @@ def filter(self, condition: "ColumnOrName") ->
"DataFrame":
Parameters
----------
condition : :class:`Column` or str
- a :class:`Column` of :class:`types.BooleanType`
+ A :class:`Column` of :class:`types.BooleanType`
or a string of SQL expressions.
Returns
-------
:class:`DataFrame`
- Filtered DataFrame.
+ A new DataFrame with rows that satisfy the condition.
Examples
--------
>>> df = spark.createDataFrame([
- ... (2, "Alice"), (5, "Bob")], schema=["age", "name"])
+ ... (2, "Alice", "Math"), (5, "Bob", "Physics"), (7, "Charlie",
"Chemistry")],
+ ... schema=["age", "name", "subject"])
Filter by :class:`Column` instances.
>>> df.filter(df.age > 3).show()
- +---+----+
- |age|name|
- +---+----+
- | 5| Bob|
- +---+----+
+ +---+-------+---------+
+ |age| name| subject|
+ +---+-------+---------+
+ | 5| Bob| Physics|
+ | 7|Charlie|Chemistry|
+ +---+-------+---------+
>>> df.where(df.age == 2).show()
- +---+-----+
- |age| name|
- +---+-----+
- | 2|Alice|
- +---+-----+
+ +---+-----+-------+
+ |age| name|subject|
+ +---+-----+-------+
+ | 2|Alice| Math|
+ +---+-----+-------+
Filter by SQL expression in a string.
>>> df.filter("age > 3").show()
- +---+----+
- |age|name|
- +---+----+
- | 5| Bob|
- +---+----+
+ +---+-------+---------+
+ |age| name| subject|
+ +---+-------+---------+
+ | 5| Bob| Physics|
+ | 7|Charlie|Chemistry|
+ +---+-------+---------+
>>> df.where("age = 2").show()
- +---+-----+
- |age| name|
- +---+-----+
- | 2|Alice|
- +---+-----+
+ +---+-----+-------+
+ |age| name|subject|
+ +---+-----+-------+
+ | 2|Alice| Math|
+ +---+-----+-------+
+
+ Filter by multiple conditions.
+
+ >>> df.filter((df.age > 3) & (df.subject == "Physics")).show()
+ +---+----+-------+
+ |age|name|subject|
+ +---+----+-------+
+ | 5| Bob|Physics|
+ +---+----+-------+
+ >>> df.filter((df.age == 2) | (df.subject == "Chemistry")).show()
+ +---+-------+---------+
+ |age| name| subject|
+ +---+-------+---------+
+ | 2| Alice| Math|
+ | 7|Charlie|Chemistry|
+ +---+-------+---------+
+
+ Filter by multiple conditions using SQL expression.
+
+ >>> df.filter("age > 3 AND name = 'Bob'").show()
+ +---+----+-------+
+ |age|name|subject|
+ +---+----+-------+
+ | 5| Bob|Physics|
+ +---+----+-------+
+
+ Filter using the :func:`Column.isin` function.
+
+ >>> from pyspark.sql.functions import col
Review Comment:
Sure!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]