Get first element from PySpark data frame

Viewed 46

I have a PySpark data frame which only contains one element. How can I extract the number from the data frame?

For the example, how can I get the number 5.0 from the PySpark data frame?

+-----------------+
|        count    |
+-----------------+
|        5.0      |
+-----------------+
2 Answers

Several alternatives:

df.head()[0]
df.head().count
df.head()['count']

first does the same, but head is 1 letter shorter ;)

sc.parallelize([5.0]).collect()

[5.0]
Related