返回一个新的数据帧省略具有 null 或 NaN 值的行。
DataFrame.dropna 是 DataFrameNaFunctions.drop 彼此的别名。
Syntax
dropna(how: str = "any", thresh: Optional[int] = None, subset: Optional[Union[str, Tuple[str, ...], List[str]]] = None)
参数
| 参数 | 类型 | 说明 |
|---|---|---|
how |
str,可选,默认为“any” | 可以是“any”或“all”的值。 如果为“any”,则删除一行(如果包含任何 null)。 如果为“all”,则仅当其所有值均为 null 时,才删除一行。 |
thresh |
int、optional、default None | 如果指定,则删除小于 thresh 非 null 值的行。 这会覆盖 how 参数。 |
subset |
str、元组或列表,可选 | 要考虑的列名的可选列表。 |
退货
DataFrame:仅排除 null 行的数据帧。
示例
from pyspark.sql import Row
df = spark.createDataFrame([
Row(age=10, height=80.0, name="Alice"),
Row(age=5, height=float("nan"), name="Bob"),
Row(age=None, height=None, name="Tom"),
Row(age=None, height=float("nan"), name=None),
])
df.na.drop().show()
# +---+------+-----+
# |age|height| name|
# +---+------+-----+
# | 10| 80.0|Alice|
# +---+------+-----+
df.na.drop(how='all').show()
# +----+------+-----+
# | age|height| name|
# +----+------+-----+
# | 10| 80.0|Alice|
# | 5| NaN| Bob|
# |NULL| NULL| Tom|
# +----+------+-----+
df.na.drop(thresh=2).show()
# +---+------+-----+
# |age|height| name|
# +---+------+-----+
# | 10| 80.0|Alice|
# | 5| NaN| Bob|
# +---+------+-----+