Skip to main content
 首页 » 编程设计

python之Numpy 数组之附加命名列

2024年10月24日12jackei

Numpy 允许我这样做:

>>>data = np.genfromtxt(r'd:/temp/data.txt', delimiter = ',', names = True) 
>>>data['Time'] 
array([ 1.,  2.,  3.]) 

如何制作这样的数组?我的意思是这样写:

data = np.array([]) 
data.append(name = 'Time', data = [1., 2., 3.]) 
data['Time'] 
array([ 1.,  2.,  3.]) 

请您参考如下方法:

您正在尝试创建一个 Record Array/Structured Array ,您可以结帐numpy.core.records.fromrecords -

In [35]: data = np.core.records.fromrecords([[1.], [2.], [3.]],names=['Time']) 
 
In [36]: data 
Out[36]: 
rec.array([(1.0,), (2.0,), (3.0,)], 
      dtype=[('Time', '<f8')]) 
 
In [37]: data['Time'] 
Out[37]: array([ 1.,  2.,  3.])