代码之家  ›  专栏  ›  技术社区  ›  Peter Krauss

如何在数据帧选择中添加中间列?

  •  0
  • Peter Krauss  · 技术社区  · 5 年前

    使用Spark 2.2和Spark shell,并尝试两者 toDF toDS .

    case class Person(name: String, age: Long)
    val df = Seq( Person("Michael", 0), Person("Andy", 30), Person("Justin", 19) ).toDS()
    

    df.select("age", "name").show
    df.select("age", "name").withColumn("foo",lit("-")).show
    

    this syntax 不起作用:

    df.select( "age", lit("-").as("foo"), "name" ).show
    

    我需要更改列的顺序并添加一些中间列。

    0 回复  |  直到 5 年前
        1
  •  1
  •   Peter Krauss    5 年前

            df.withColumn("foo",lit("-")). select("name","foo","age")