如何将存储为字典键的数据帧连接到单个数据帧中?

时间:2018-11-06 17:32:51

标签: python pandas

我有一本包含120个键的字典,每个键都包含一个数据帧。如何通过编程方式(使用pd.concat将它们连接成单个大数据帧?

1 个答案:

答案 0 :(得分:1)

使用pd.concat(d.values())简单地连接字典的值:

>>> d = {1:pd.DataFrame(np.random.random((5,5))),2:pd.DataFrame(np.random.random((5,5)))}
>>> d
{1:           0         1         2         3         4
0  0.319556  0.540776  0.988554  0.775070  0.535067
1  0.383192  0.379474  0.204998  0.948605  0.785190
2  0.006732  0.362755  0.537260  0.854110  0.409386
3  0.795973  0.073652  0.796565  0.168206  0.814202
4  0.531702  0.524501  0.002366  0.631852  0.024509, 

2:           0         1         2         3         4
0  0.369098  0.125491  0.832362  0.183199  0.729110
1  0.069843  0.337424  0.476837  0.078589  0.489447
2  0.504904  0.456996  0.239802  0.025953  0.609697
3  0.262001  0.646389  0.992928  0.124552  0.878561
4  0.707881  0.520429  0.609257  0.018488  0.167053}

>>> new_df = pd.concat(d.values())
>>> new_df
          0         1         2         3         4
0  0.319556  0.540776  0.988554  0.775070  0.535067
1  0.383192  0.379474  0.204998  0.948605  0.785190
2  0.006732  0.362755  0.537260  0.854110  0.409386
3  0.795973  0.073652  0.796565  0.168206  0.814202
4  0.531702  0.524501  0.002366  0.631852  0.024509
0  0.369098  0.125491  0.832362  0.183199  0.729110
1  0.069843  0.337424  0.476837  0.078589  0.489447
2  0.504904  0.456996  0.239802  0.025953  0.609697
3  0.262001  0.646389  0.992928  0.124552  0.878561
4  0.707881  0.520429  0.609257  0.018488  0.167053