我想找到一种好的方法将一个2D的numpy数组转化为带有列名和行名的结构化数组。例如:
import numpy as np
column_names = ['a', 'b', 'c']
row_names = ['1', '2', '3']
matrix = np.reshape((1, 2, 3, 4, 5, 6, 7, 8, 9), (3, 3))
# TODO: insert magic here
matrix['3']['a'] # 7
我已经能够像这样设置列:
matrix.dtype = [(n, matrix.dtype) for n in column_names]
这使得我可以执行matrix[2]['a']
,但现在我想重命名行,以便我可以执行matrix['3']['a']
。