PySpark DataFrame 的 dropna(~) 方法删除具有缺失值的行。
参数
1.how | string | optional
-
如果是
'any',则删除包含任何空值的行。 -
如果是
'all',则删除包含所有空值的行。
默认情况下,how='any' 。
2. thresh | int | optional
删除非空值少于 thresh 的行。请注意,这会覆盖 how 参数。
3. subset | string 或 tuple 或 list | optional
要检查空值的行。默认情况下,将检查所有行。
返回值
PySpark 数据帧。
例子
考虑以下PySpark DataFrame:
df = spark.createDataFrame([["Alex", 20], [None, None], ["Cathy", None]], ["name", "age"])
df.show()
+-----+----+
| name| age|
+-----+----+
| Alex| 20|
| null|null|
|Cathy|null|
+-----+----+
删除 PySpark DataFrame 中至少有一个缺失值的行
要删除至少有一个缺失值的行:
df.dropna().show()
+----+---+
|name|age|
+----+---+
|Alex| 20|
+----+---+
删除 PySpark DataFrame 中至少有 n 个非缺失值的行
要删除至少包含 2 个非缺失值的行:
n_non_missing_vals = 2
df.dropna(thresh=n_non_missing_vals).show()
+----+---+
|name|age|
+----+---+
|Alex| 20|
+----+---+
删除 PySpark DataFrame 中至少有 n 个缺失值的行
要删除至少有 2 个缺失值的行:
n_missing_vals = 2
df.dropna(thresh=len(df.columns)-n_missing_vals+1).show()
+-----+----+
| name| age|
+-----+----+
| Alex| 20|
|Cathy|null|
+-----+----+
删除 PySpark DataFrame 中所有缺失值的行
要删除包含所有缺失值的行:
df.dropna(how='all').show()
+-----+----+
| name| age|
+-----+----+
| Alex| 20|
|Cathy|null|
+-----+----+
删除 PySpark DataFrame 中缺少某些值的行
要删除缺少 age 值的行:
df.dropna(subset='age').show()
+----+---+
|name|age|
+----+---+
|Alex| 20|
+----+---+
删除 PySpark DataFrame 中缺少某些值的行
要删除缺少 name 或 age 列值的行:
df.dropna(subset=['name','age'], how='any').show()
+----+---+
|name|age|
+----+---+
|Alex| 20|
+----+---+
删除 PySpark DataFrame 中某些值缺失(全部)的行
要删除 name 和 age 列值都缺失的行:
df.dropna(subset=['name','age'], how='all').show()
+-----+----+
| name| age|
+-----+----+
| Alex| 20|
|Cathy|null|
+-----+----+
相关用法
- Python Pandas DataFrame dropna方法用法及代码示例
- Python PySpark DataFrame drop方法用法及代码示例
- Python PySpark DataFrame dropDuplicates方法用法及代码示例
- Python Pandas DataFrame drop_duplicates方法用法及代码示例
- Python Pandas DataFrame drop方法用法及代码示例
- Python Pandas DataFrame droplevel方法用法及代码示例
- Python PySpark DataFrame dtypes属性用法及代码示例
- Python Pandas DataFrame dtypes属性用法及代码示例
- Python Pandas DataFrame duplicated方法用法及代码示例
- Python Pandas DataFrame diff方法用法及代码示例
- Python Pandas DataFrame dot方法用法及代码示例
- Python Pandas DataFrame describe方法用法及代码示例
- Python PySpark DataFrame describe方法用法及代码示例
- Python Pandas DataFrame div方法用法及代码示例
- Python PySpark DataFrame distinct方法用法及代码示例
- Python Pandas DataFrame empty属性用法及代码示例
- Python Pandas DataFrame pop方法用法及代码示例
- Python Pandas DataFrame nsmallest方法用法及代码示例
- Python Pandas DataFrame sample方法用法及代码示例
- Python Pandas DataFrame items方法用法及代码示例
- Python Pandas DataFrame max方法用法及代码示例
- Python Pandas DataFrame swaplevel方法用法及代码示例
- Python Pandas DataFrame agg方法用法及代码示例
- Python Pandas DataFrame copy方法用法及代码示例
- Python Pandas DataFrame pow方法用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 PySpark DataFrame | dropna method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
