无法解析spark 2.4中的symbol.withcolumn

hfyxw5xn  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(439)

Spark:2.4
数据框包含每个员工的平均登录时间

AverageLoginHour|employee
3.392265193     |emp_1
2.833333333     |emp_2
5.638888889     |emp_3
6.909090909     |emp_4
7.361445783     |emp_5

代码:

tds.select("Employee","AverageLoginHour")
    (count("AverageLoginHour").alias("logincnt"))
    (sum("AverageLoginHour").alias("loginsum"))
      .withColumn("TotalEmployeeavg",col("loginsum")/col("logincnt")*100)

Error: Cannot resolve symbol .withcolumn

预期产量:

AverageLoginHour|   employee    Totalavg|Remarks
3.392265193     |    Emp_1      |5.2    |Below Avg
2.833333333     |    Emp_2      |5.2    |Below Avg
5.638888889     |    Emp_3      |5.2    |Above Avg
6.909090909     |    Emp_4      |5.2    |Above Avg
7.361445783     |    Emp_5      |5.2    |Above Avg

如果员工平均登录时间小于totalavg than,则列备注如下平均值,否则高于平均值。
请分享你的建议。

ecr0jaav

ecr0jaav1#

使用 avg 内置函数 window 本案的条款。 Example: ```
df.show()
//+----------------+--------+
//|AverageLoginHour|employee|
//+----------------+--------+
//| 3.392265193| emp_1|
//| 2.833333333| emp_2|
//| 5.638888889| emp_3|
//| 6.909090909| emp_4|
//| 7.361445783| emp_5|
//+----------------+--------+

df.withColumn("Totalavg",avg(col("AverageLoginHour")).over()).
withColumn("Remarks",when(col("Totalavg") > col("AverageLoginHour"),lit("Below Avg")).otherwise(lit("Above Avg"))).
show()

//+----------------+--------+------------+---------+
//|AverageLoginHour|employee| Totalavg| Remarks|
//+----------------+--------+------------+---------+
//| 3.392265193| emp_1|5.2270048214|Below Avg|
//| 2.833333333| emp_2|5.2270048214|Below Avg|
//| 5.638888889| emp_3|5.2270048214|Above Avg|
//| 6.909090909| emp_4|5.2270048214|Above Avg|
//| 7.361445783| emp_5|5.2270048214|Above Avg|
//+----------------+--------+------------+---------+

//rounding to 1
df.withColumn("Totalavg",round(avg(col("AverageLoginHour")).over(),1)).withColumn("Remarks",when(col("Totalavg") > col("AverageLoginHour"),lit("Below Avg")).otherwise(lit("Above Avg"))).show()
//+----------------+--------+--------+---------+
//|AverageLoginHour|employee|Totalavg| Remarks|
//+----------------+--------+--------+---------+
//| 3.392265193| emp_1| 5.2|Below Avg|
//| 2.833333333| emp_2| 5.2|Below Avg|
//| 5.638888889| emp_3| 5.2|Above Avg|
//| 6.909090909| emp_4| 5.2|Above Avg|
//| 7.361445783| emp_5| 5.2|Above Avg|
//+----------------+--------+--------+---------+

另一种方法是不使用窗口函数和杠杆 `crossJoin` . `Example:` ```
val df1=df.selectExpr("avg(AverageLoginHour) as Totalavg")
df.crossJoin(df1).
withColumn("Remarks",when(col("Totalavg") > col("AverageLoginHour"),lit("Below Avg")).otherwise(lit("Above Avg"))).
show()
//+----------------+--------+------------+---------+
//|AverageLoginHour|employee|    Totalavg|  Remarks|
//+----------------+--------+------------+---------+
//|     3.392265193|   emp_1|5.2270048214|Below Avg|
//|     2.833333333|   emp_2|5.2270048214|Below Avg|
//|     5.638888889|   emp_3|5.2270048214|Above Avg|
//|     6.909090909|   emp_4|5.2270048214|Above Avg|
//|     7.361445783|   emp_5|5.2270048214|Above Avg|
//+----------------+--------+------------+---------+

相关问题