我有一个 pandas.DataFrame ,如下所示,您可以将其读取为 pd.DataFrame(data_dict):
data_dict =
{'Elevation': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1},
'Azimuth': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 0, 6: 1, 7: 2, 8: 3, 9: 4},
'median': {0: 255,
1: 255,
2: 255,
3: 255,
4: 255,
5: 256,
6: 256,
7: 256,
8: 256,
9: 256},
'count': {0: 255,
1: 255,
2: 255,
3: 255,
4: 255,
5: 250,
6: 250,
7: 250,
8: 250,
9: 250},
'to_drop': {0: 1,
1: 1,
2: 1,
3: 1,
4: 1,
5: 0,
6: 0,
7: 0,
8: 0,
9: 0}}
我想将其转换为 numpy 中的 3D 矩阵。 3D 矩阵的形状为
[Azimuth.nunique(), Elevation.nunique(), 3(Median,count,to_drop)]
,即 [5,2,3]。
我尝试过
data.groupby(['Elevation','Azimuth']).apply(lambda x: x.values).reset_index().values
结果是 10,3 数组。如何得到5,2,3数组?
IIUC用途:
out = df.pivot(index='Elevation', columns='Azimuth').stack(0).to_xarray().to_array()
print (out)
<xarray.DataArray (variable: 5, Elevation: 2, level_1: 3)>
array([[[255, 255, 1],
[250, 256, 0]],
[[255, 255, 1],
[250, 256, 0]],
[[255, 255, 1],
[250, 256, 0]],
[[255, 255, 1],
[250, 256, 0]],
[[255, 255, 1],
[250, 256, 0]]], dtype=int64)
Coordinates:
* Elevation (Elevation) int64 0 1
* level_1 (level_1) object 'count' 'median' 'to_drop'
* variable (variable) int32 0 1 2 3 4
print (out.shape)
(5, 2, 3)