嵌套字典到数据框

时间:2018-07-02 10:11:52

标签: python pandas dictionary dataframe

我有一个嵌套的字典,看起来像这样(这是很短的版本):

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'}

我想拥有一个像这样的excel

enter image description here

我正在尝试:

    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

显示错误:

”对于curr_dict [k2] [k3]中的k4: TypeError:“ float”对象不可迭代” 任何想法如何将内部值更改为字符串,显然它们都是浮点数。

谢谢!

1 个答案:

答案 0 :(得分:0)

按照@Fozoro的建议,您可以按照以下步骤操作:

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]
相关问题