从具有列表作为项目的嵌套字典构造pandas DataFrame [英] Construct pandas DataFrame from nested dictionaries having list as item

查看:71
本文介绍了从具有列表作为项目的嵌套字典构造pandas DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个字典数据,我想转换成Pandas DataFrame.但是,由于不必要的键"0"(对我而言),当我将这些字典转换为DataFrame时,我获得了DataFrame的不期望格式.实际上,这些命令只是整个数据的一小部分.

I have several dictionary data and I want to convert to Pandas DataFrame. However, due to unnecessary key '0' (for me), I've obtained undesirable format of DataFrame when I convert these dict to DataFrame. Actually, these dicts are short part of whole data.

dict1 = {1: {0: [-0.022, -0.017]},
         2: {0: [0.269, 0.271]},
         3: {0: [0.118, 0.119]},
         4: {0: [0.057, 0.061]},
         5: {0: [-0.916, -0.924]}}

dict2 = {1: {0: [0.384, 0.398]},
         2: {0: [0.485, 0.489]},
         3: {0: [0.465, 0.469]},
         4: {0: [0.456, 0.468]},
         5: {0: [-0.479, -0.482]}}

dict3 = {1: {0: [-0.323, -0.321]},
         2: {0: [-0.535, -0.534]},
         3: {0: [-0.336, -0.336]},
         4: {0: [-0.140, -0.142]},
         5: {0: [0.175, 0.177]}}

DataFrame(dict1)

                  1               2               3               4  \
0  [-0.022, -0.017]  [0.269, 0.271]  [0.118, 0.119]  [0.057, 0.061]   

                  5  
0  [-0.916, -0.924]  

我已经使用"for"迭代解决了这个问题,结果是我最终想要获得的结果.

I've solved this problem using 'for' iteration and the result is what I want to obtain finally.

    index = [['dict1', 'dict1', 'dict2', 'dict2', 'dict3', 'dict3'], ['A', 'B']*3]
    dict = DataFrame(index = index)
    for k in dict1.keys():
        dict = dict.join(DataFrame(dict1[k][0]+dict2[k][0]+dict3[k][0], index = index, columns = [k]))

    print dict

             1      2      3      4      5
dict1 A -0.022  0.269  0.118  0.057 -0.916
      B -0.017  0.271  0.119  0.061 -0.924
dict2 A  0.384  0.485  0.465  0.456 -0.479
      B  0.398  0.489  0.469  0.468 -0.482
dict3 A -0.323 -0.535 -0.336 -0.140  0.175
      B -0.321 -0.534 -0.336 -0.142  0.177

但是,当我将此方法应用于整个数据长度时,我等不及要等到该操作完成.我也找到了使用面板"的方法.它减少了时间,但还不满意.

However, when I apply this method to whole length of data, I couldn't wait until the operation was done. I've also found method using 'Panel'. It reduced the time but not satisfied yet.

pd.Panel.from_dict(dict1).to_frame()

请让我知道解决此简单问题的最佳方法.

Please let me know the best way for this simple problem.

推荐答案

您应该简单地从嵌套dict中删除一个级别,以使生活更轻松.下面的代码删除了不必要的字典部分,并将每个字典的数据帧连接在一起.

You should simply drop a level from your nested dict to make life easier. The code below drops the unnecessary part of your dicts and concatenates the dataframes from each of the dicts together.

all_dicts=[dict1,dict2,dict3]
df=pd.concat([pd.DataFrame({k:v[0] for k,v in d.items()}) for d in all_dicts])
df.index=pd.MultiIndex.from_product([['dict1','dict2','dict3'],['A','B']])

>>> df 
             1      2      3      4      5
dict1 A -0.022  0.269  0.118  0.057 -0.916
      B -0.017  0.271  0.119  0.061 -0.924
dict2 A  0.384  0.485  0.465  0.456 -0.479
      B  0.398  0.489  0.469  0.468 -0.482
dict3 A -0.323 -0.535 -0.336 -0.140  0.175
      B -0.321 -0.534 -0.336 -0.142  0.177

这篇关于从具有列表作为项目的嵌套字典构造pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆