1

I have the following Python / Pyspark code:

sql_command = ''' query ''''
df = spark.sql(sql_command)
ls_colnames = df.schema.names
ls_colnames
     ['id', 'level1', 'level2', 'level3', 'specify_facts']

cSchema = StructType([
    StructField("colname", StringType(), False)
  ])
df_colnames = spark.createDataFrame(dataset_array,schema=cSchema)

File "/opt/mapr/spark/spark-2.1.0/python/pyspark/sql/types.py", line 1366, in _verify_type raise TypeError("StructType can not accept object %r in type %s" % (obj, type(obj))) TypeError: StructType can not accept object 'id' in type class 'str'

What can I do to get a spark object of the colnames? `

1 Answer 1

3

Not sure if I have understood your question correctly. But if you are tryng to create a dataframe based on the given list, you can use below code for the same.

from pyspark.sql import Row
l =  ['id', 'level1', 'level2', 'level3', 'specify_facts']
rdd1 = sc.parallelize(l)
row_rdd = rdd1.map(lambda x: Row(x))
sqlContext.createDataFrame(row_rdd,['col_name']).show()

Hope it Helps.

Regards,

Neeraj

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

Comments

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.