Input: I have Excel file containing 3 columns and format of excel file is like as follow:
A C D
A C E
A F G
B H J
B H K
A F I
B L M
B L N
A F O
I wish to make dictionary from the above input in below format: Output:
dictionary= {'A':{'C':['D','E'],'F':['G','I','O']},'B':{'H':['J','K'],'L':['M','N']}}
Logic: For each distinct column-1 value, need to make nested dictionary & in that nested part, for each distinct column-2 value, need to make list of correspondingly column-3 values.
You can do it like so with pandas:
Output:
@Edchum @MYGz
Thanks!! But without using pandas, i ended by doing something like this.
nested_dict(2,list) is the main important point in the code which allowed me to specify two levels of nesting.
If you find anything better or alternative of Pre-defining the structure of nested dictionary in python, please share with example.