【问题标题】:Adding Percentage sign to dataframe in scala and writing to excel在scala中向数据框添加百分比符号并写入excel
【发布时间】:2021-02-08 18:25:08
【问题描述】:

我想使用 scala 将数据框写入 excel。

    val noErrors = 20000
    val total = 1000
    val emails = Seq(
      (1, "first", noErrors/100),
      (2, "SPAM SPAM spam", total),
      (3, "Hello, We'd like to", noErrors)
    ).toDF("id", "text","text2")
      .withColumn("text2",
        when($"text2" === lit(-1.0), lit(null)  )
          .otherwise(round($"text2", 1)))
      .orderBy($"id".asc)
      .drop("id")
    emails.show()

我正在尝试将 % 符号添加到“noErrors/100”。没有任何条件,它可以正常工作,但是当我添加条件列时,它会出错。

【问题讨论】:

    标签: scala dataframe apache-spark-sql


    【解决方案1】:
    import org.apache.spark.sql.functions._
    val noErrors = 20000
        val total = 1000
        val emails = Seq(
          (1, "first", noErrors/100),
          (2, "SPAM SPAM spam", total),
          (3, "Hello, We'd like to", noErrors)
        ).toDF("id", "text","text2")
          .withColumn("text2",
            when($"text2" === lit(-1.0), lit(null)  )
              .otherwise(round($"text2", 1)))
          .orderBy($"id".asc)
          .drop("id")
    
    val df = emails.withColumn("text2", when(col("text") === "first","200%") )
    df.show()
    

    输出:

    +-------------------+-----+
    |               text|text2|
    +-------------------+-----+
    |              first| 200%|
    |     SPAM SPAM spam| null|
    |Hello, We'd like to| null|
    +-------------------+-----+
    
    import org.apache.spark.sql.functions._
    noErrors: Int = 20000
    total: Int = 1000
    emails: org.apache.spark.sql.DataFrame = [text: string, text2: int]
    df: org.apache.spark.sql.DataFrame = [text: string, text2: string]
    

    这是你想要的吗?

    【讨论】:

      猜你喜欢
      • 2016-06-10
      • 2018-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      • 2017-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多