2017-05-18 4 views
0

데이터 프레임에 CSV를로드하고 SPARK SQL을 사용하여 작업을 수행했습니다.Dataframe의 열에서 비율을 찾는 방법

"올바름"또는 "잘못된"값이있는 열 이름 결정이 있습니다.

나는 올바른 결정의 비율/총 결정을 찾고 싶다. 조각 아래

답변

0

는 ... 솔루션

scala> val df = sc.parallelize(Seq((1,"right"),(2,"right"),(3,"right"),(4,"wrong"))).toDF("col1","col2") 
df: org.apache.spark.sql.DataFrame = [col1: int, col2: string] 

scala> df.registerTempTable("test_table") 
warning: there was one deprecation warning; re-run with -deprecation for details 

scala> spark.sql("""SELECT sum(CASE WHEN col2 = "right" THEN 1 ELSE 0 END)/count(*) as percentage FROM test_table""").show() 
+----------+ 
|percentage| 
+----------+ 
|  0.75| 
+----------+ 
+0

감사를 올바른 결정의 비율을 얻을 스파크 SQL을 사용 – codelover

관련 문제