I am new in Spark. And when I use toDF() function to convert RDD to dataframe, it seems to compute all the transformation function like map() I've written before. I wonder if toDF() in PySpark is a transformation or an action.
I create a simple RDD and use a simple function to output its value, just for test, And use toDF() after map(). The result seems to run the function in map partially. And when I show the result of dataframe, toDF() act like transformation and output the result again.
>>> a = sc.parallelize([(1,),(2,),(3,)])
>>> def f(x):
... print(x[0])
... return (x[0] + 1, )
...
>>> b = a.map(f).toDF(["id"])
2
1
>>> b = a.map(f).toDF(["id"]).show()
2
1
1
2
3
+---+
| id|
+---+
| 2|
| 3|
| 4|
+---+
Could someone tell me why toDF() function in PySpark act both like action and transformation? Thanks a lot.
PS: In Scala, toDF act like transformation in my case.