'Build a subnested dictionary from a Pandas DataFrame

Alright, this is perhaps something rather simple. I want to know how to generate a nested structure of objects(recursive dictionary). Something like this:

{
   top_parent_child:
      child: {
         child:{
            child:{}
         }
      }
}

There is a point where the object has no more child objects. Now, to be more specific, I'm getting this data from an excel file and processing it with Pandas. The file has the following shape:

object_id parent_pbject
ABC123
BCD123 ABC123
BCD345 ABC123
BCD234 BCD123
BCD567 BCD234
ABC789 BCD234
ABCD12 ABC789

I want this:

{
   ABC123: {
      BCD345: {}
      BCD123:{
         BCD234:{
            ABC789:{
               ABCD12:{}
            }
            BCD567:{}
         }
      }
   }
}

One child might have multiple sub-childs and so on. There are many suggestions I have received for this, like exporting a JSON using Pandas and then run through the entire things to recursively build the dictionary. But IMO the best option is to use Pandas with "groupby". For brevity, I used df.groupby(['parent_object']) after doing all the required filtering in the file because it is pretty big. After all that is done, I export the result as a dict, but it doesn't quite have the structure I'm looking for.

Do you have any better approach for this with Pandas? I'll appreciate any insight.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source