从混乱的字典数据结构中获取数据帧



我有一本字典,如下所示:

{1:            ds       yhat  yhat_lower  yhat_upper
30 2015-08-09  49.908927   31.632462   66.742083
31 2015-08-16  49.750056   34.065527   67.069122
32 2015-08-23  49.591185   32.620258   67.403908
33 2015-08-30  49.432314   32.257891   67.541757
34 2015-09-06  72.395618   55.612973   89.711030
35 2015-09-13  49.114572   32.199945   66.255518
36 2015-09-20  48.955701   30.759960   66.118051,
2:            ds       yhat  yhat_lower  yhat_upper
30 2015-08-09  38.001931   23.583157   51.291784
31 2015-08-16  37.922999   25.370967   50.504328
32 2015-08-23  37.844068   23.743860   51.143868
33 2015-08-30  37.765136   24.903955   50.309284
34 2015-09-06  39.227773   25.089493   52.719935
35 2015-09-13  37.607273   24.370609   51.313454
36 2015-09-20  37.528341   23.395560   50.499454}

想要获取类似此输出的数据帧

ProductCode    ds         yhat       yhat_lower  yhat_upper
1           2015-08-09  49.908927   31.632462   66.742083
1           2015-08-16  49.750056   34.065527   67.069122
1           2015-08-23  49.591185   32.620258   67.403908
1           2015-08-30  49.432314   32.257891   67.541757
1           2015-09-06  72.395618   55.612973   89.711030
1           2015-09-13  49.114572   32.199945   66.255518
1           2015-09-20  48.955701   30.759960   66.118051,
2           2015-08-09  38.001931   23.583157   51.291784
2           2015-08-16  37.922999   25.370967   50.504328
2           2015-08-23  37.844068   23.743860   51.143868
2           2015-08-30  37.765136   24.903955   50.309284
2           2015-09-06  39.227773   25.089493   52.719935
2           2015-09-13  37.607273   24.370609   51.313454
2           2015-09-20  37.528341   23.395560   50.499454

我失败的尝试: new_df = pd。DataFrame(df.items((, columns=['ProductCode', 'yhat']( 打印(new_df(

ProdutCode    yhat
0  1        ds yhat yhat_lower yhat_upp...
1  2        ds yhat yhat_lower yhat_upp...

在字典中,所有的 ds yhat yhat_lower 和 yhat_upper head 及其值都被视为 dict.values((。如何将这些字母部分分隔为数据帧列,而数值部分是它们自己的列值?

让我们试试pd.concat

yourdf=pd.concat(d).reset_index(level=0)

最新更新