How do I collect a single column in Spark?

后端 未结 1 2158
天涯浪人
天涯浪人 2021-02-19 08:41

I would like to perform an action on a single column. Unfortunately, after I transform that column, it is now no longer a part of the dataframe it came from but a Column object.

1条回答
  •  庸人自扰
    2021-02-19 09:04

    Spark >= 2.0

    Starting from Spark 2.0.0 you need to explicitly specify .rdd in order to use flatMap

    df.select("array").rdd.flatMap(lambda x: x).collect()
    

    Spark < 2.0

    Just select and flatMap:

    df.select("array").flatMap(lambda x: x).collect()
    ## [[1, 2, 3]] 
    

    0 讨论(0)
提交回复
热议问题