所以我在解析数据集时遇到问题。我有一个尺寸为axbxc的数组。我想将其转换为二维数组,最好是形状为axb [c]的DataFrame,这意味着我想要c的条目作为b不同列下的列表,其中a表示行数,如下所示。
我假设您想从3d数组制作数据帧,因此Iam在这种情况下使用multiindex。如果我有这样的数据:
data = np.random.random((3,4,5))
首先,我将像这样为数据帧创建索引:
#for index DataFrame
array1 = np.arange(0,3)
array2 = np.arange(0,4)
iterables = [array1.tolist(),array2.tolist()]
index = pd.MultiIndex.from_product(iterables, names=['first', 'second'])
然后创建数据框:
df = pd.DataFrame(data.reshape(3*4,5),index=index)
和结果:
>>> df
0 1 2 3 4
first second
0 0 0.824310 0.504847 0.524925 0.864275 0.737136
1 0.568694 0.349271 0.413173 0.256493 0.062059
2 0.247119 0.433598 0.916364 0.579064 0.749974
3 0.404534 0.951233 0.511670 0.147814 0.662542
1 0 0.982126 0.914015 0.093291 0.756780 0.610224
1 0.853450 0.463260 0.712753 0.874551 0.118973
2 0.365568 0.822954 0.883047 0.398301 0.496278
3 0.329698 0.929337 0.085680 0.312687 0.210055
2 0 0.973088 0.131381 0.943814 0.596567 0.248843
1 0.986318 0.631151 0.671049 0.123051 0.362313
2 0.109261 0.008249 0.129571 0.702480 0.786857
3 0.426099 0.322008 0.260797 0.435768 0.453475
>>>