python – 将两个字典合并为一个数据帧

这是两个词典:

monkeydict = {'16:43': 1, '16:44': 1, '16:49': 3}
pigdict = {'16:41': 3, '16:44': 2, '16:51': 3}

这是所需的数据帧:

time,monkeydict,pigdict
16:41,,3
16:43,1,
16:44,1,2
16:49,3,
16:51,,3

最佳答案

pd.DataFrame({'monkeydict':pd.Series(monkeydict),'pigdict':pd.Series(pigdict)})
Out[879]: 
       monkeydict  pigdict
16:41         NaN      3.0
16:43         1.0      NaN
16:44         1.0      2.0
16:49         3.0      NaN
16:51         NaN      3.0

或者(请注意,您需要在创建数据框后重命名列)

pd.DataFrame([monkeydict,pigdict]).T
Out[887]: 
         0    1
16:41  NaN  3.0
16:43  1.0  NaN
16:44  1.0  2.0
16:49  3.0  NaN
16:51  NaN  3.0
点赞