I feel very confused with DataFrame.from_dict. It seems DataFrame.from_dict works right on dict with tuple dict accidentally @_@. Seriously, I have tried some cases below.
case1:
dict_var1 = {('a', 'b'): [43,47], ('a', 'c'): [23,40], ('b', 'c'): [30,35], ('c', 'd'): [10,30], ('a', 'd'): [23,28]}
frame_var1 = pd.DataFrame.from_dict(dict_var1, orient='index', columns=['num', 'cnt'])
print(frame_var1.index)
result1:
Index([('a', 'b'), ('a', 'c'), ('b', 'c'), ('c', 'd'), ('a', 'd')], dtype='object')
case2:
dict_var2 = {('a', 'b'): {'num': 43, 'cnt': 47}, ('a', 'c'): {'num': 23, 'cnt': 40}, ('b', 'c'): {'num': 30, 'cnt': 35}, ('c', 'd'): {'num': 10, 'cnt': 30}, ('a', 'd'): {'num': 23, 'cnt': 28}}
frame_var2 = pd.DataFrame.from_dict(dict_var2, orient='index', columns=['num', 'cnt'])
print(frame_var2.index)
result2:
MultiIndex(levels=[['a', 'b', 'c'], ['b', 'c', 'd']], labels=[[0, 0, 0, 1, 2], [0, 1, 2, 1, 2]])
question:
When I change the type of dict values from list to dict, DataFrame.from_dict can change the tuple dict keys into multi-index right. Could anyone explain how DataFrame.from_dict works leading to these results?