I didn't find any pyspark code to convert matrix to spark dataframe except the following example using Scala. Does anyone know how to use python instead?
How to convert a mllib matrix to a spark dataframe?
I didn't find any pyspark code to convert matrix to spark dataframe except the following example using Scala. Does anyone know how to use python instead?
How to convert a mllib matrix to a spark dataframe?
We can use toArray()
method to convert DenseMatrix to numpy ndarray and tolist()
to convert from array to list.
>>> m = DenseMatrix(2, 2, range(4))
>>> m
DenseMatrix(2, 2, [0.0, 1.0, 2.0, 3.0], False)
>>> rows = m.toArray().tolist()
>>> rows
[[0.0, 2.0], [1.0, 3.0]]
>>> df = spark.createDataFrame(rows,['col1','col2'])
>>> df.show()
+----+----+
|col1|col2|
+----+----+
| 0.0| 2.0|
| 1.0| 3.0|
+----+----+