【问题标题】:DataFrame error: “overloaded method value select with alternatives”DataFrame 错误:“使用替代方法选择重载方法值”
【发布时间】:2017-02-12 14:31:36
【问题描述】:

我尝试通过从数据框中选择小时+分钟/60 和其他列来创建一个新的数据框,如下所示:

val logon11 = logon1.select("User","PC","Year","Month","Day","Hour","Minute",$"Hour"+$"Minute"/60)

我收到以下错误:

<console>:38: error: overloaded method value select with alternatives:
  (col: String,cols: String*)org.apache.spark.sql.DataFrame <and>
  (cols: org.apache.spark.sql.Column*)org.apache.spark.sql.DataFrame
cannot be applied to (String, String, String, String, String, String, String,org.apache.spark.sql.Colum)

...

也许我知道原因是我无法同时使用“select”获得具有这些类型的 DataFrame。那我怎样才能得到这样的数据框呢?

【问题讨论】:

  • 旁注:如果你不小心把“$”放在select中,它会给出同样的错误

标签: scala apache-spark dataframe


【解决方案1】:

DF 的select 方法采用所有Strings 或所有org.apache.spark.sql.Columns 类型的参数,但不会同时采用两者。

在您的情况下,您将StringColumn 类型参数都传递给select 方法。

val logon11 = logon1.select($"User",$"PC",$"Year",$"Month",$"Day",$"Hour",$"Minute",$"Hour"+$"Minute"/60 as "total_hours")

希望对你有帮助!

【讨论】:

    【解决方案2】:

    您可以使用withColumn 从现有列或基于以下某些条件创建新列

    val logon1 = Seq(("User1","PC1",2017,2,12,12,10)).toDF("User","PC","Year","Month","Day","Hour","Minute")
    val logon11 = logon1.withColumn("new_col", $"Hour"+$"Minute"/60)
    logon11.printSchema()
    logon11.show
    

    输出:

    root
     |-- User: string (nullable = true)
     |-- PC: string (nullable = true)
     |-- Year: integer (nullable = false)
     |-- Month: integer (nullable = false)
     |-- Day: integer (nullable = false)
     |-- Hour: integer (nullable = false)
     |-- Minute: integer (nullable = false)
     |-- new_col: double (nullable = true)
    
    
    +-----+---+----+-----+---+----+------+------------------+
    | User| PC|Year|Month|Day|Hour|Minute|           new_col|
    +-----+---+----+-----+---+----+------+------------------+
    |User1|PC1|2017|    2| 12|  12|    10|12.166666666666666|
    +-----+---+----+-----+---+----+------+------------------+
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      • 2018-05-17
      • 1970-01-01
      • 2017-08-04
      相关资源
      最近更新 更多