【问题标题】:filter pyspark on multiple conditions using AND OR使用 AND OR 在多个条件下过滤 pyspark
【发布时间】:2021-04-04 13:05:18
【问题描述】:

我的 df 中有以下两列。我想过滤这些列,使过滤后的结果 df 应该像下面的结果 df。

输入表

col1 col2
null Approved
FALSE null
null null
FALSE Approved
TRUE null
null new
FALSE Declined
FALSE new

过滤后的输出结果表

col1 col2
null Approved
null null
**FALSE Approved**
TRUE null
null new

逻辑 - 如果 col1 为 False,则 col2 应获得批准,或者 col1 不应等于 FALSE。

【问题讨论】:

    标签: apache-spark pyspark apache-spark-sql


    【解决方案1】:

    您可以在您的df 上使用where

    df.where("""
     (
        col1='FALSE' AND 
        col2='Approved'
     ) OR
     col1 <> 'FALSE'
    """)
    

    df.where(
        (
            (df.col1 == 'FALSE') &
            (df.col2 == 'Approved')
        ) 
        | 
        (df.col1 != 'FALSE')
    )
    

    注意。我们使用&amp; 代表and| 代表or

    【讨论】:

      猜你喜欢
      • 2018-12-25
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 2016-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多