Mir*_*ola 3 null filter pyspark
假设我们有一个简单的数据框:
from pyspark.sql.types import *
schema = StructType([
StructField('id', LongType(), False),
StructField('name', StringType(), False),
StructField('count', LongType(), True),
])
df = spark.createDataFrame([(1,'Alice',None), (2,'Bob',1)], schema)
Run Code Online (Sandbox Code Playgroud)
问题是如何检测空值?我尝试了以下方法:
df.where(df.count == None).show()
df.where(df.count is 'null').show()
df.where(df.count == 'null').show()
Run Code Online (Sandbox Code Playgroud)
它导致错误:
condition should be string or Column
Run Code Online (Sandbox Code Playgroud)
我知道以下作品:
df.where("count is null").show()
Run Code Online (Sandbox Code Playgroud)
但是有没有办法在没有完整字符串的情况下实现?即df.count……?
另一种方法是使用filterapi
from pyspark.sql import functions as F
df.filter(F.isnull("count")).show()
Run Code Online (Sandbox Code Playgroud)
您可以使用 Spark 函数 isnull
from pyspark.sql import functions as F
df.where(F.isnull(F.col("count"))).show()
Run Code Online (Sandbox Code Playgroud)
或者直接用方法 isNull
df.where(F.col("count").isNull()).show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30035 次 |
| 最近记录: |