-4

Hi I want to make a nested dictionary into a data frame

've looked up other answers for a nested dictionary to data frame but it doesn't seem to be working.

d= {​​​​"info":{​​​​"nest":{​​​​"pno":"abc","c":"US","moreinfo":{​​​​"a":"Xlab","b":"miner","currency":"USD","data":0,"make":"new","value":"false","infolang":"[]","Type":"null","Desc":"poppy(a)","tera":"0-982","population":432,"additionalpop":"1 M","price":600,"log":"J01","additionalinfomore":[{​​​​"date":"01012000","tera":"AGREED_AGE","paintnumber":"p","displayFromLink":"true","additionalinfomorev":{​​​​"phone":"null","email":"null","connection":"noconnection"}​​​​,"cached":"null","blob":"null","clarify":"ok"}​​​​​​​}​​​​}

its so many nested dictionaries..

I tried the following:

df.columns=df.column.map(lambda x :x.split(".")[-1])
df

worked only partially.

Can anyone please help?

Sid
  • 23
  • 6
  • How do you want your dataframe to look like? How does it look like now? The code you've provided doesn't relate to transforming the dictionary into a dataframe at all. – Yevhen Kuzmovych Jul 16 '21 at 14:07
  • I want all the keys to be columns and all values as values in the columns – Sid Jul 16 '21 at 14:18
  • 2
    Please, edit your question to include the expected result. The dictionaries and dataframes are inherently different data structures. So it's very unclear how your output should look like. – Yevhen Kuzmovych Jul 16 '21 at 14:20

1 Answers1

1

The only way that I would know to solve this problem is to use nested for loops until you access the levels that you wish to edit. There are probably much better solutions than this one, but this is one I know.

You first create a list containing keys of your primary dictionary:

keys_list = list(dict.keys())

Then you would have to enter each layer of keys by accessing the keys of the nested dictionary. It would look something like:

for key in keys_list:
    inner_keys = dict[key].keys()
    for inner_key in inner_keys:
            df = pd.DataFrame(dict[key][inner_key])

You unfortunately need to access the base levels using loops. You need to use N-1 loops in order get to whatever level you have your information nested in. In your case, it looks like you have a 4-dimensional dictionary, from how I'm interpreting it. So you would require 3 loops to access your information, which means that you would have an algorithm that has a big O of n cubed.

This means that if it normally takes an operation 3 seconds to run through your first level keys, it will take approximately 27 seconds to get to your lower levels of data. This algorithm would be incredibly inefficient in accessing your data.

After 3 or 4 dimensions, it might be better to try something different. A better approach might be to instantiate separate 3 dimensional dictionaries which you can call later.

Please let me know if you have questions.