0

I have a Data Frame as below

+-----+---+------+-----+
| id |age|height| score
+-----+---+------+-----+
|1001|  5|    80| 12
|1002|  9|    95| 189
|1003| 10|    82| 345
+-----+---+------+-----+

and want to create new column which combines all other columns in a key value structure and few columns as it is something like below

+-----+----------------------------------------------------------+------+
| id  |property                                                  | score
+-----+----------------------------------------------------------+------+
|1001|  {'id': '1001', 'age': '5', 'height': '80', 'score': '12'} | 12
|1002|  {'id': '1002', 'age': '9', 'height': '95', 'score': '189'}|189
|1003| {'id': '1003', 'age': '10', 'height': '82', 'score':'345'}|345
+----------------------------------------------------------------+--------+

I tried with df.withColumn('property', map(lambda row: row.asDict(), df.collect())) but it is not producing results as I want. Anything wrong with my approach?

1 Answer 1

1

You can do it using to_json and struct functions.

df = df.select(
    'id',
    F.to_json(F.struct('*')).alias('property'),
    'score'
)
df.show(truncate=False)
Sign up to request clarification or add additional context in comments.

5 Comments

It includes escape character additionally in String columns is there any way to avoid it? I think its because of to_json otherwise can I use to_dict or some other options?
Can you provide some sample data?
One of the column has json structure already like '{"mark": {"Sub1": {"type": "Taal"}, "Sub2": {"type": "WO"}, "Sub3": {"type": "Numbers"}' when I do to_json again it includes '\' in the place of "".
Gotta get off work. Tomorrow, let's see how the json type field is concatenated with the to_json result.
Solved !! df.withColumn('property', F.regexp_replace('property', '\\\\', ''))

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.