简体   繁体   中英

Pyspark dataframe get all values of a column

I want to get all values of a column in pyspark dataframe. I did some search, but I never find a efficient and short solution.

Assuming I want to get a values in the column called "name". I have a solution:

sum(dataframe.select("name").toPandas().values.tolist(),[])

It works, but it is not efficient since it converts to pandas then flatten the list... Is there a better and short solution?

Below Options will give better performance than sum .

Using collect_list

import pyspark.sql.functions as f
my_list = df.select(f.collect_list('name')).first()[0]

Using RDD:

my_list = df.select("name").rdd.flatMap(lambda x: x).collect()

I am not certain but in my couple of stress test, collect_list gives better performance. Will be great if someone can confirm.

To get all values of a column in a list we can use collect() -

column_value_list = [row['<column_name>'] for row in df.select('<column_name>').collect()]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM