将列名字符串分配给2d numpy数组列



我有一个NumPy数组(它有2000列,要大得多)

1 4 7
2 5 8
3 6 9

我想在这个数组中添加一串值作为列标题,以便以类似于下面的方式使用文本对数组进行索引:

import numpy as np
array = np.genfromtxt("data.txt", dtype=None, encoding=None, delimiter="t").reshape(-1, 3).T
#  Sting of headers to insert
namesstring = ['a100','bt200','c300']
# Need to assign hear to numpy columns like this
print(array)
a100 b200 c300
1    4    7
2    5    8
3    6    9

请注意,原始数据位于我使用np.genfromtxt作为NumPy数组导入的文本文件中。如何将字符串作为列索引名称分配给numpy数组?

仅在numpy中使用vstack()方法:-

array=np.vstack((namesstring,array))

最新更新