This is how I create a dataframe with primitive data types in pyspark:
from pyspark.sql.types import StructType, StructField, DoubleType, StringType, IntegerType
fields = [StructField('column1', IntegerType(), True), StructField('column2', IntegerType(), True)]
schema = StructType(fields)
df = spark.createDataFrame([], schema)
values = [tuple([i]) +
tuple([i])
for i in range(3)]
df = spark.createDataFrame(values, schema)
Now, if I want to have a third column with dictionary data, eg: {"1": 1.0, "2": 2.0, "3": 3.0}, what should I do? I want to create this data frame:
+--------------------+-----------------+------------------------------+
|column1 |column2 |column3 |
+--------------------+-----------------+------------------------------+
|1 |1 |{"1": 1.0, "2": 1.0, "3": 1.0}|
+--------------------+-----------------+------------------------------+
|2 |2 |{"1": 2.0, "2": 2.0, "3": 2.0}|
+--------------------+-----------------+------------------------------+
|3 |3 |{"1": 3.0, "2": 3.0, "3": 3.0}|
+--------------------+-----------------+------------------------------+
There is a MapType that seems to be helpful, but I can't figure out how to use it?
And assuming the data frame is created, how to filter it based on the third column, given a dict to select the rows of the data frame that have that dict value?