【问题标题】:converting column type in dataframe in Spark [duplicate]在 Spark 中转换数据框中的列类型 [重复]
【发布时间】:2018-10-29 02:19:01
【问题描述】:

我在 spark 中有一个数据框:

column1 | column2
-------------------
 a         1
 b         2

column1 和column2 都是字符串类型。

如何将 column2 从字符串转换为大整数?

【问题讨论】:

    标签: apache-spark dataframe


    【解决方案1】:

    您只需要将列转换为 bigint 或 long(在 Spark 中也是如此)

    val df = sc
        .parallelize(Seq(("a", "1"), ("b", "2")))
        .toDF("A", "B")
    df.printSchema
    root
         |-- A: string (nullable = true)
         |-- B: string (nullable = true)
    
    
    df.withColumn("B", 'B cast "bigint").printSchema
    

    df.withColumn("B", 'B cast "long").printSchema
    root
         |-- A: string (nullable = true)
         |-- B: long (nullable = true)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-29
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多