Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I convert a list of lists in a Dataframe in Pyspark?

I have a list of list of type:

[[1, 'A', 'aa'], [2 'B', 'bb'], [3, 'C', 'cc']]

I want to get the next dataframe:

id col1 col2
1 A aa
2 B bb
3 C cc

This solution is somewhat similar ,but list has data from same columns
https://stackoverflow.com/a/46891396/6463651

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just use the list to create the dataframe directly.

data = [[1, 'A', 'aa'], [2, 'B', 'bb'], [3, 'C', 'cc']]
df = spark.createDataFrame(data, ['id', 'col1', 'col2'])
df.show(truncate=False)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading