python - 具有多个条件的 Sparksql 过滤(使用 where 子句选择)

标签 python sql apache-spark apache-spark-sql pyspark

您好,我有以下问题:

numeric.registerTempTable("numeric"). 

我要过滤的所有值都是文字空字符串,而不是 N/A 或 Null 值。

我尝试了这三个选项:

  1. numeric_filtered = numeric.filter(numeric['LOW'] != 'null').filter(numeric['HIGH'] != 'null').filter(numeric['NORMAL' ] != 'null')

  2. numeric_filtered = numeric.filter(numeric['LOW'] != 'null' AND numeric['HIGH'] != 'null' AND numeric['NORMAL'] != 'null' )

  3. sqlContext.sql("SELECT * from numeric WHERE LOW != 'null' AND HIGH != 'null' AND NORMAL != 'null'")

    /li>

不幸的是,numeric_filtered 总是空的。我检查了一下,numeric 有应该根据这些条件过滤的数据。

以下是一些示例值:

低高正常

3.5 5.0 空

2.0 14.0 空

空 38.0 空

空空空空

1.0 空 4.0

最佳答案

您正在使用逻辑连词 (AND)。这意味着所有列都必须不同于 'null' 才能包含行。让我们以 filter version 为例说明:

numeric = sqlContext.createDataFrame([
    ('3.5,', '5.0', 'null'), ('2.0', '14.0', 'null'),  ('null', '38.0', 'null'),
    ('null', 'null', 'null'),  ('1.0', 'null', '4.0')],
    ('low', 'high', 'normal'))

numeric_filtered_1 = numeric.where(numeric['LOW'] != 'null')
numeric_filtered_1.show()

## +----+----+------+
## | low|high|normal|
## +----+----+------+
## |3.5,| 5.0|  null|
## | 2.0|14.0|  null|
## | 1.0|null|   4.0|
## +----+----+------+

numeric_filtered_2 = numeric_filtered_1.where(
    numeric_filtered_1['NORMAL'] != 'null')
numeric_filtered_2.show()

## +---+----+------+
## |low|high|normal|
## +---+----+------+
## |1.0|null|   4.0|
## +---+----+------+

numeric_filtered_3 = numeric_filtered_2.where(
    numeric_filtered_2['HIGH'] != 'null')
numeric_filtered_3.show()

## +---+----+------+
## |low|high|normal|
## +---+----+------+
## +---+----+------+

您尝试过的所有其余方法都遵循完全相同的架构。您在这里需要的是逻辑析取 (OR)。

from pyspark.sql.functions import col 

numeric_filtered = df.where(
    (col('LOW')    != 'null') | 
    (col('NORMAL') != 'null') |
    (col('HIGH')   != 'null'))
numeric_filtered.show()

## +----+----+------+
## | low|high|normal|
## +----+----+------+
## |3.5,| 5.0|  null|
## | 2.0|14.0|  null|
## |null|38.0|  null|
## | 1.0|null|   4.0|
## +----+----+------+

或使用原始 SQL:

numeric.registerTempTable("numeric")
sqlContext.sql("""SELECT * FROM numeric
    WHERE low != 'null' OR normal != 'null' OR high != 'null'"""
).show()

## +----+----+------+
## | low|high|normal|
## +----+----+------+
## |3.5,| 5.0|  null|
## | 2.0|14.0|  null|
## |null|38.0|  null|
## | 1.0|null|   4.0|
## +----+----+------+

另请参阅:Pyspark: multiple conditions in when clause

关于python - 具有多个条件的 Sparksql 过滤(使用 where 子句选择),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33747834/

相关文章:

python - 从base64代码保存图片

python - Tkinter - 如何在文本框的开头插入文本?

同时匹配两个条件的 SQL Where 子句

sql - Oracle 如何执行 OR 条件验证?

python - 用于二进制分类的 spark 逻辑回归 : apply new threshold for predicting 2 classes

python - 使用 python 打打印新 stdout 行时出现问题

python - 将字典转换为可以转换的 float

sql - mysql中的if子句

hadoop - YARN 上的 Spark : execute driver without worker

json - 由于数据类型不匹配而获取 : argument 2 requires integral type error while parsing Json data Spark SQL