使用矢量化操作从表格到(图像)数组

问题描述 投票:0回答:1

有没有办法从表中获取数据,例如

axis0  axis1  value
0      0      2
1      1      1
0      1      3

进入(图像)数组

[[2 3]
[nan 1]]

使用矢量化运算或类似的东西?是的,数据中缺少一些点,那么应该有 nan 或等效值。

现在我正在使用 for 循环将值插入到所有值均为 nan 的数组中,但是当数据较多时,处理数据的速度相当慢......

python pandas numpy vectorization
1个回答
0
投票

在轴列上旋转数据框,然后转换为 numpy

df.pivot(index='axis0', columns='axis1', values='value').to_numpy()

array([[ 2.,  3.],
       [nan,  1.]])
© www.soinside.com 2019 - 2024. All rights reserved.