Skip to content
Advertisement

Find all values that one column’s value has and collect as JSON

class_id class code id
8 XYZ A 1
8 XYZ B 2
9 ABC C 3

I have a dataframe like above. I want to transform it so the ‘codes’ column below collects all the unique (code, id) pairs into a JSON format that a class contains.

class_id class codes
8 XYZ [{‘code: ‘A’, ‘id’: 1}, {‘code’: ‘B’, ‘id’: 2}]
9 ABC [{‘code: ‘C’, ‘id’: 3}]

Advertisement

Answer

This approach should works for you:

JavaScript

Output:
enter image description here

Advertisement