如何爆炸(嵌套)嵌套数据框?

时间:2020-07-14 18:32:10

标签: python-3.x pandas dataframe nested

我有一列中有一个嵌套DataFrame的熊猫DataFrame,我想爆炸嵌套的列,但无法弄清楚如何实现此目的,我读过一些有关如何取消嵌套的文章-nest list对象,但似乎不适用于pandas数据框,我有以下代码:

df = pd.DataFrame([[datetime(2020,1,2), 334, pd.DataFrame(
                                                [[datetime(2020,1,3), 555],
                                                [datetime(2020,1,4), 678]],
                                                columns=['date', 'value'],
                                                index=['Italy']*2
                                            )], 
                  [datetime(2020,1,3), 332, pd.DataFrame(
                                                [[datetime(2020,1,3), 555],
                                                [datetime(2020,1,4), 678]],
                                                  columns=['date', 'value'],
                                                  index=['US']*2
                                            )],
                  [datetime(2020,1,4), 115, pd.DataFrame(
                                                [[datetime(2020,1,3), 555],
                                                [datetime(2020,1,4), 678]],
                                                  columns=['date', 'value'],
                                                  index=['Germany']*2
                                            )]
                  ],
                 columns=['date', 'value', 'nested_values'],
                 index=['Italy', 'US', 'Germany'])

预期结果应该是这样的:

+--------+------------+-----+
+Italy   + 2020-01-02 + 334 +
+Italy   + 2020-01-03 + 555 +
+Italy   + 2020-01-04 + 678 +
+US      + 2020-01-03 + 332 +
+US      + 2020-01-03 + 555 +
+US      + 2020-01-04 + 678 +
+Germany + 2020-01-04 + 115 +
+Germany + 2020-01-03 + 555 +
+Germany + 2020-01-04 + 678 +
+--------+------------+-----+

0 个答案:

没有答案