Convert distinct values in a Dataframe in Pyspark to a list

Viewed 8386

I'm trying to get the distinct values of a column in a dataframe in Pyspark, to them save them in a list, at the moment the list contains "Row(no_children=0)" but I need only the value as I will use it for another part of my code.

So, ideally only all_values=[0,1,2,3,4]

all_values=sorted(list(df1.select('no_children').distinct().collect()))
all_values


[Row(no_children=0),
 Row(no_children=1),
 Row(no_children=2),
 Row(no_children=3),
 Row(no_children=4)]

This takes around 15secs to run, is that normal?

Thank you very much!

2 Answers
Related