Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- +---+--------+----+
- | Id| Size| Amt|
- +---+--------+----+
- | a1| 1|55.0|
- | a2| 2|48.0|
- | a3| 3|28.0|
- +---+--------+----+
- StructType([
- StructField("Id", StringType(), True),
- StructField("Size", IntegerType(), True),
- StructField("Amt", FloatType(), True)
- ])
- {"Id":"a1", "Size":1, "Amt":55.0}
- {"Id":"a2", "Size":2, "Amt":48.0}
- {"Id":"a3", "Size":3, "Amt":28.0}
- {"Id":"a1", "Size":1, "Amt":55.0, "Arr":[{"Id":"a1","Size":1,"Amt":55.0 }] }
- {"Id":"a2", "Size":2, "Amt":48.0, "Arr":[{"Id":"a2","Size":2,"Amt":48.0 }] }
- {"Id":"a3", "Size":3, "Amt":28.0, "Arr":[{"Id":"a3","Size":3,"Amt":28.0 }] }
- df1 = df.select('Id', 'Size', 'Amt', array('Id','Size','Amt').alias("Arr"))
- df1.write.json("my_new_output_path")
- {"Id":"a1", "Size":1, "Amt":55.0, "Arr":["a1", 1 ,55.0] }
- {"Id":"a2", "Size":2, "Amt":48.0, "Arr":["a2", 2 ,48.0] }
- {"Id":"a3", "Size":3, "Amt":28.0, "Arr":["a3", 3 ,28.0] }
- df1 = df.select('Id', 'Size', 'Amt', create_map(lit('Id'), 'Id', lit('Size'), 'Size', lit('Amt'), 'Amt').alias("Arr"))
- {"Id":"a1","Size":1,"Amt":55.0,"Arr":{"Id":"a1","Size":"1","Amt":"55.0"}}
- {"Id":"a2","Size":2,"Amt":48.0,"Arr":{"Id":"a2","Size":"2","Amt":"48.0"}}
- {"Id":"a3","Size":3,"Amt":28.0,"Arr":{"Id":"a3","Size":"3","Amt":"28.0"}}
Add Comment
Please, Sign In to add comment