I am struggling to transform my pyspark dataframe which looks like this:
df = spark.createDataFrame([('0018aad4',[300, 450], ['{"v1": "blue"}', '{"v2": "red"}']), ('0018aad5',[300], ['{"v1": "blue"}'])],[ "id","Tlist", 'Tstring'])
df.show(2, False)
+--------+----------+-------------------------------+
|id |Tlist |Tstring |
+--------+----------+-------------------------------+
|0018aad4|[300, 450]|[{"v1": "blue"}, {"v2": "red"}]|
|0018aad5|[300] |[{"v1": "blue"}] |
+--------+----------+-------------------------------+
to this:
df_result = spark.createDataFrame([('0018aad4',[300, 450], 'blue', 'red'), ('0018aad5',[300], 'blue', None)],[ "id","Tlist", 'v1', 'v2'])
df_result.show(2, False)
+--------+----------+----+----+
|id |Tlist |v1 |v2 |
+--------+----------+----+----+
|0018aad4|[300, 450]|blue|red |
|0018aad5|[300] |blue|null|
+--------+----------+----+----+
I tried to pivot and a bunch of others things but don't get the result above.
Note that I don't have the exact number of dict in the column Tstring
Do you know how I can do this?