0

I have a nested dictionary that looks like this(this is pretty shortened version):

my_dict = {('rs1', rn1): {u'rs2': u'rs3', u'rs4': u'rs5', u'rs6': u'rs7', u'rs8': u'rs9', u'rs10t': u'rs11', u'rs12': u'rs13', u'rs14': u'rs15', u'rs16': u'rs17', u'rs18': u'rs19'}, ('rs21', rn2): {u'rs22': u'rs23'}, ('rs24', rn2): {u'rs25': u'rs26', u'rs27': u'rs28', u'rs29': u'rs30'}

And I would like to have an excel that looks like

enter image description here

Im trying with:

    new_list = []
for k1 in remove_empties_from_dict(combined_dict):
     curr_dict = remove_empties_from_dict(combined_dict)[k1]

     for k2 in curr_dict:
         new_dict = {'col1': k1, 'col2': k2}
         for k3 in curr_dict[k2]:
             new_dict = {'col1': k1, 'col2': k2, 'col3': k3}
             for k4 in curr_dict[k2][k3]:
               new_dict= {'col1': k1, 'col2': k2, 'col3': k3, 'col4': k4}                  

new_list.append(new_dict)
df = pd.DataFrame(new_list)
print df

It says an error:

"for k4 in curr_dict[k2][k3]: TypeError: 'float' object is not iterable" Any idea how to change inner values to strings, apparently they are floats.

Thank you!

Noob Programmer
  • 698
  • 2
  • 6
  • 22
  • Possible duplicate of [Construct pandas DataFrame from items in nested dictionary](https://stackoverflow.com/questions/13575090/construct-pandas-dataframe-from-items-in-nested-dictionary) – Nazim Kerimbekov Jul 02 '18 at 10:42
  • I checked it out, but its not, since he puts rs_2 on top of the column. On index values, I would like to have it in the table. – Noob Programmer Jul 02 '18 at 11:11

1 Answers1

0

As @Fozoro suggests, you can do it with the following steps:

my_dct = {('rs1', "rn1"): {'rs2': 'rs3', 'rs4': 'rs5', 'rs6': 'rs7', 
                           'rs8': 'rs9', 'rs10t': 'rs11', 'rs12': 'rs13', 
                           'rs14': 'rs15', 'rs16': 'rs17', 'rs18': 'rs19'}, 
          ('rs21', "rn2"): {'rs22': 'rs23'}, 
          ('rs24', "rn3"): {'rs25': 'rs26', 'rs27': 'rs28', 'rs29': 'rs30'}}
newdf = pd.DataFrame.from_dict({(i,j): my_dct[i][j] 
                               for i in my_dct.keys() 
                               for j in my_dct[i].keys()},
                                orient='index').rename(columns={0:"Column 4"})
newdf["Column 1"] = newdf.index.str[0].str[0]
newdf["Column 2"] = newdf.index.str[0].str[1]
newdf["Column 3"] = newdf.index.str[1]
J. Doe
  • 3,458
  • 2
  • 24
  • 42