I would like to do json tree that inside have multiple of parent, you will see my example dataframe in column child_id , rows to be parent must be equal to 0 and it will be Color and Mobile and each children will bind id to parent.
My data
import pandas as pd
data = {
'id': ['2', '13', '14', '15', '16', '17', '18'],
'name': ['color', 'red', 'blue', 'ruby red', 'mobile', 'iphone', 'sumsung'],
'child_id': ['0', '2', '2', '13', '0', '16', '16']
}
df = pd.DataFrame(data)
df
Expected Output
[
{
'name': 'color',
'id': 2,
'child_id': 0,
'children': [
{
'name': 'red',
'id': 13,
'child_id': 2,
'children': [
{
'name': 'ruby red',
'id': 15,
'child_id': 13,
},
],
},
{
'name': 'blue',
'id': 14,
'child_id': 2,
},
],
},
{
'name': 'mobile',
'id': 16,
'child_id': 0,
'children': [
{
'name': 'iphone',
'id': 17,
'child_id': 16,
},
{
'name': 'samsung',
'id': 18,
'child_id': 16,
},
],
},
]
