'structtype add method to add arraytype

structtype has a method call add. i see example to use

schema = Structype()
schema.add('testing',string)
schema.add('testing2',string)

how can I add Structype and array type in the schema , using add()?



Solution 1:[1]

You need to use it as below -

from pyspark.sql.types import *

schema = StructType()
schema.add('testing',StringType())
schema.add('testing2',StringType())

Sample example to create a dataframe using this schema -

df = spark.createDataFrame(data=[(1,2), (3,4)],schema=schema)
df.show()

+-------+--------+
|testing|testing2|
+-------+--------+
|      1|       2|
|      3|       4|
+-------+--------+

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1