0

I have a hierarchical data(more than 10 generation) which tells who a person's parent/children are. i would want to represent this as dict of dict. is there any way to achieve this.

sample input - List of Dict/Dataframe

[{'Name': 'Oli Bob', 'Location': 'United Kingdom', 'Parent': nan}, {'Name': 'Mary May', 'Location': 'Germany', 'Parent': 'Oli Bob'}, {'Name': 'Christine Lobowski', 'Location': 'France', 'Parent': 'Oli Bob'}, {'Name': 'Brendon Philips', 'Location': 'USA', 'Parent': 'Oli Bob'}, {'Name': 'Margret Marmajuke', 'Location': 'Canada', 'Parent': 'Brendon Philips'}, {'Name': 'Frank Harbours', 'Location': 'Russia', 'Parent': 'Brendon Philips'}, {'Name': 'Todd Philips', 'Location': 'United Kingdom', 'Parent': 'Frank Harbours'}, {'Name': 'Jamie Newhart', 'Location': 'India', 'Parent': nan}, {'Name': 'Gemma Jane', 'Location': 'China', 'Parent': nan}, {'Name': 'Emily Sykes', 'Location': 'South Korea', 'Parent': 'Emily Sykes'}, {'Name': 'James Newman', 'Location': 'Japan', 'Parent': nan}]

same data in table form

enter image description here

Desired Output

[
    {name:"Oli Bob", location:"United Kingdom", _children:[
        {name:"Mary May", location:"Germany"},
        {name:"Christine Lobowski", location:"France"},
        {name:"Brendon Philips", location:"USA",_children:[
            {name:"Margret Marmajuke", location:"Canada"},
            {name:"Frank Harbours", location:"Russia",_children:[{name:"Todd Philips", location:"United Kingdom"}]},
        ]},
    ]},
    {name:"Jamie Newhart", location:"India"},
    {name:"Gemma Jane", location:"China", _children:[
        {name:"Emily Sykes", location:"South Korea"},
    ]},
    {name:"James Newman", location:"Japan"},
];
rakesh
  • 326
  • 7
  • 19
  • 3
    Check this post, similar to yours: [Given a flat list of (parent,child), create a hierarchical dictionary tree](https://stackoverflow.com/questions/45460653/given-a-flat-list-of-parent-child-create-a-hierarchical-dictionary-tree) – Ank May 18 '21 at 07:14
  • [This answer](https://stackoverflow.com/a/42588587/7109869) may also be of help. – Gonçalo Peres May 18 '21 at 11:00

0 Answers0