熊猫:如何连接在数据框上有价值的字典?

时间:2020-06-29 20:50:10

标签: python pandas tuples

我在数据框上有一个嵌套的字典,值如下:

report = Dict[str, Tuple(pd.DataFrame, pd.DataFrame)]

例如我的数据是

report["A"][0] = 
   example_id  rmse_center_x  rmse_center_y
0           0        3.5e-02        2.9e-02
1           1        4.5e-02        8.8e-02
2           2        5.2e-02        7.0e-02
3           3        7.6e-02        6.5e-02

report["A"][1] = 
     example_id frame_id  gt_center_x  hp_center_x  gt_center_y  hp_center_y
0             0       20        -15.1        -15.1        -11.8        -11.8
1             0       21        -15.2        -15.2        -11.8        -11.8
2             0       22        -15.3        -15.3        -11.8        -11.8
3             0       23        -15.5        -15.4        -11.8        -11.8
4             0       24        -15.5        -15.5        -11.8        -11.8

report["B"][0] = 
   example_id  rmse_center_x  rmse_center_y
0           0        5.0e-02        6.4e-02
1           1        6.2e-02        8.2e-02
2           2        8.4e-02        5.9e-02
3           3        8.7e-02        9.1e-02

report["B"][1] = 
     example_id frame_id  gt_center_x  hp_center_x  gt_center_y  hp_center_y
0             0       20        -15.1        -15.0        -11.8        -11.8
1             0       21        -15.2        -15.1        -11.8        -11.8
2             0       22        -15.3        -15.3        -11.8        -11.8
3             0       23        -15.5        -15.5        -11.8        -11.8
4             0       24        -15.5        -15.6        -11.8        -11.8

我将在for循环中接收report数据结构。在for循环的每一步中,我得到了report["A"]report["B"]

output = {"A": pd.DataFrame, "B": pd.DataFrame()}
for batch_id in range(10):
   report = some_func()
   # report has four items:
   # 1. report["A"][0]
   # 2. report["A"][1]
   # 3. report["B"][0]
   # 4. report["B"][1]

   output["A"]["metrics"] = concatenate(report["A"][0])
   output["A"]["raw_events"] = concatenate(report["A"][1])
   output["B"]["metrics"] = concatenate(report["B"][0])
   output["B"]["raw_events"] = concatenate(report["B"][1])

如何拥有output字典,该字典将上面提到的这4个数据场中的每一个与batch_id的列连接在一起 例如

output["A"]["raw_events"].columns = 
     batch_id  example_id frame_id  gt_center_x  hp_center_x  gt_center_y  hp_center_y   

0 个答案:

没有答案
相关问题