我有一个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].

我已经try 了data.groupby(['Elevation','Azimuth']).apply(lambda x: x.values).reset_index().values个结果在10,3array.如何获得5,2,3数组?

推荐答案

首先用DataFrame.stack旋转DataFrame.pivot,然后将MultiIndex DataFrame转换为3d数组-首先用DataFrame.to_xarray:

out = (df.pivot(index='Elevation', columns='Azimuth').stack(level=0, future_stack=True)
         .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)

另一个 idea 是使用numpy.reshapenumpy.transpose:

df1 = df.pivot(index='Elevation', columns='Azimuth').stack(level=0, future_stack=True)

out = df1.to_numpy().reshape(*df1.index.levshape,-1).transpose(2, 0, 1)
print (out)
[[[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]]]

Python相关问答推荐

提取两行之间的标题的常规表达

pandas DataFrame GroupBy.diff函数的意外输出

重新匹配{ }中包含的文本,其中文本可能包含{{var}

Python json.转储包含一些UTF-8字符的二元组,要么失败,要么转换它们.我希望编码字符按原样保留

使用miniconda创建环境的问题

在Python Attrs包中,如何在field_Transformer函数中添加字段?

如何在polars(pythonapi)中解构嵌套 struct ?

运输问题分支定界法&

将JSON对象转换为Dataframe

如何在图中标记平均点?

isinstance()在使用dill.dump和dill.load后,对列表中包含的对象失败

启动带有参数的Python NTFS会导致文件路径混乱

如何在BeautifulSoup/CSS Select 器中处理regex?

Python—压缩叶 map html作为邮箱附件并通过sendgrid发送

python的文件. truncate()意外地没有截断'

如何使用pytest在traceback中找到特定的异常

浏览超过10k页获取数据,解析:欧洲搜索服务:从欧盟站点收集机会的微小刮刀&

Python OPCUA,modbus通信代码运行3小时后出现RuntimeError

如何在SQLAlchemy + Alembic中定义一个"Index()",在基表中的列上

在聚合中使用python-polars时如何计算模式