How to access values in array column?

后端 未结 4 749
眼角桃花
眼角桃花 2021-02-04 03:18

I have a Dataframe with one column. Each row of that column has an Array of String values:

Values in my Spark 2.2 Dataframe

[\"123\", \"abc\", \"2017\         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-04 04:25

    you can do something like below

    import org.apache.spark.sql.functions._
    
    val ds = Seq(
     Array("123", "abc", "2017", "ABC"),
     Array("456", "def", "2001", "ABC"),
     Array("789", "ghi", "2017", "DEF")).toDF("col")
    
    ds.withColumn("col1",element_at('col,1))
    .withColumn("col2",element_at('col,2))
    .withColumn("col3",element_at('col,3))
    .withColumn("col4",element_at('col,4))
    .drop('col)
    .show()
    
    +----+----+----+----+
    |col1|col2|col3|col4|
    +----+----+----+----+
    | 123| abc|2017| ABC|
    | 456| def|2001| ABC|
    | 789| ghi|2017| DEF|
    +----+----+----+----+
    

提交回复
热议问题