嵌套字典到datafram

2024-04-19 18:31:02 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我想要一个

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

上面写着一个错误:

“对于k4,按货币[k2][k3]: TypeError:“float”对象不可iterable 知道如何将内部值更改为字符串吗,显然它们是浮点数。你知道吗

谢谢你!你知道吗


Tags: innewfork2k1dictremovelist
1条回答
网友
1楼 · 发布于 2024-04-19 18:31:02

正如@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]

相关问题 更多 >