0

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.

enter image description here

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,
      },
    ],
  },
]

1 Answer 1

1

You can do this without pandas. Just iterate the initial data to create a dictionary, keyed by id, and with corresponding objects (dictionaries) as values.

Then iterate the data again to link these objects to their parents.

Here is how it could be done:

def makehiearchy(data):
    result = []
    d = { "0": { "children": result } }
    for id, name, child_id in zip(data["id"], data["name"], data["child_id"]):
        d[id] = { "name": name, "id": id, "child_id": child_id }
    
    for id, child_id in zip(data["id"], data["child_id"]):
        parent = d[child_id]
        if "children" not in parent:
            parent["children"] = []
        parent["children"].append(d[id])

    return result

# Example run
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']
}
hierarchy = makehiearchy(data)
Sign up to request clarification or add additional context in comments.

Comments

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.