2016-03-09 81 views
16

我有一个有四个字段的数据框。其中一个字段名称是Status,我试图在.filter中为数据框使用OR条件。我尝试了下面的查询,但没有运气。火花数据帧中过滤器的多个条件

df2 = df1.filter(("Status=2") || ("Status =3")) 

df2 = df1.filter("Status=2" || "Status =3") 

有没有人使用过此之前。我在堆栈溢出here上看到过类似的问题。他们使用下面的代码来使用OR条件。但是这个代码是针对pyspark的。

from pyspark.sql.functions import col 

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

通过这条线来看:'斯卡拉>从pyspark.sql。列导入列'看起来像你试图使用pyspark代码时,你真的y使用scala –

+0

@TonyTorres是的,这是一个错误,我发现后,发布这个问题。现在进行编辑。 – dheee

回答

22

相反的:

df2 = df1.filter("Status=2" || "Status =3") 

尝试:

df2 = df1.filter($"Status" === 2 || $"Status" === 3) 
+0

谢谢。这工作。 – dheee

+3

'==='的反面是'=!=' – Boern

+2

取决于版本 - 对于2.0以前的版本,使用'!=='但在版本2.0.0之后'!=='没有相同的优先级'===',使用'=!='代替 –

0

你需要使用过滤器

package dataframe 

import org.apache.spark.sql.SparkSession 
/** 
* @author [email protected] 
*/ 
// 

object DataFrameExample{ 
    // 
    case class Employee(id: Integer, name: String, address: String, salary: Double, state: String,zip:Integer) 
    // 
    def main(args: Array[String]) { 
    val spark = 
     SparkSession.builder() 
     .appName("DataFrame-Basic") 
     .master("local[4]") 
     .getOrCreate() 

    import spark.implicits._ 

    // create a sequence of case class objects 

    // (we defined the case class above) 

    val emp = Seq( 
    Employee(1, "vaquar khan", "111 algoinquin road chicago", 120000.00, "AZ",60173), 
    Employee(2, "Firdos Pasha", "1300 algoinquin road chicago", 2500000.00, "IL",50112), 
    Employee(3, "Zidan khan", "112 apt abcd timesqure NY", 50000.00, "NY",55490), 
    Employee(4, "Anwars khan", "washington dc", 120000.00, "VA",33245), 
    Employee(5, "Deepak sharma ", "rolling edows schumburg", 990090.00, "IL",60172), 
    Employee(6, "afaq khan", "saeed colony Bhopal", 1000000.00, "AZ",60173) 
    ) 

    val employee=spark.sparkContext.parallelize(emp, 4).toDF() 

    employee.printSchema() 

    employee.show() 


    employee.select("state", "zip").show() 

    println("*** use filter() to choose rows") 

    employee.filter($"state".equalTo("IL")).show() 

    println("*** multi contidtion in filer || ") 

    employee.filter($"state".equalTo("IL") || $"state".equalTo("AZ")).show() 

    println("*** multi contidtion in filer && ") 

    employee.filter($"state".equalTo("AZ") && $"zip".equalTo("60173")).show() 

    } 
}