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]
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.