【发布时间】:2017-03-08 15:04:41
【问题描述】:
我有一个二维 NumPy 结构数组:
arr = np.zeros((3,5), [('x',int), ('y',float)])
即:
array([[(0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0)],
[(0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0)],
[(0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0)]],
dtype=[('x', '<i8'), ('y', '<f8')])
我想用它创建一个 Pandas 面板。我尝试了明显的:
pd.Panel(arr)
ValueError: The number of dimension required is 3, but the number of ndarray given was 2
然后我发现了这个可怕的一堆:
pd.Panel(dict(enumerate(pd.DataFrame(a) for a in arr)))
它产生:
<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 5 (major_axis) x 2 (minor_axis)
Items axis: 0 to 2
Major_axis axis: 0 to 4
Minor_axis axis: x to y
这“有效”,但效率非常低且令人眼花。
如何构建此类面板?
编辑:我在这里提交了一个问题:https://github.com/pandas-dev/pandas/issues/14511
【问题讨论】:
-
你想要的最终形状是什么?类似
pd.Panel(arr.reshape((1, arr.shape[0], arr.shape[1])))或pd.Panel(arr.reshape(( arr.shape[0], arr.shape[1],1)))? -
@EdChum:我在问题中写的丑陋的一堆给出的最终形状是可以的。您编写的代码确实生成了面板,但它们充满了 NaN,而不是来自
arr的数据!!我将更新问题以显示可怕堆的结果。
标签: pandas numpy panel-data