0

Having trouble converting the following list to a pyspark dataframe.

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

cols = ['col1', 'col2', 'col3']

Desired output:

    +----------+----------+----------+ 
    | col1     | col2     | col3     |
    +----------+----------+----------+ 
    | 1        | A        | aa       |
    +----------+----------+----------+ 
    | 2        | B        | bb       |
    +----------+----------+----------+ 
    | 3        | C        | cc       |
    +----------+----------+----------+ 

I'm essentially looking for the pandas equivalent of:

df = pd.DataFrame(data=lst,columns=cols)

1 Answer 1

3

If you have pandas package installed then can just import the dataframe to pyspark using spark.createDataFrame

import pandas as pd
from pyspark.sql import SparkSession


lst = [[1, 'A', 'aa'], [2, 'B', 'bb'], [3, 'C', 'cc']]
cols = ['col1', 'col2', 'col3']

df = pd.DataFrame(data=lst,columns=cols)

#Create PySpark SparkSession
spark = SparkSession.builder \
    .master("local[1]") \
    .appName("spark") \
    .getOrCreate()

#Create PySpark DataFrame from Pandas
sparkDF=spark.createDataFrame(df) 
sparkDF.printSchema()
sparkDF.show()

Alternatively, you can also do it without having pandas

from pyspark.sql import SparkSession

lst = [[1, 'A', 'aa'], [2, 'B', 'bb'], [3, 'C', 'cc']]
cols = ['col1', 'col2', 'col3']

#Create PySpark SparkSession
spark = SparkSession.builder \
    .master("local[1]") \
    .appName("spark") \
    .getOrCreate()

df = spark.createDataFrame(lst).toDF(*cols)
df.printSchema()
df.show()

Sign up to request clarification or add additional context in comments.

2 Comments

I was getting an error - most likely datatype related since spark is very particular about datatypes. That's why I was looking for a pyspark based solution (ie. the list in the question is an example)
@chicagobeast12 Could you share the error message? I've also posted an approach that doesn't require pandas. Does that help resolve the issue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.