[英]Adding columns to a structured Numpy array
I have the following data in geo.dat
我在
geo.dat
有以下数据
id lon lat inhab name
1 9. 45. 100 Ciriè
2 10. 45. 60 Acquanegra
and I get it in a ndarray
我把它放在
ndarray
import numpy as np
data = np.genfromtxt('geo.dat', dtype=None, names=True)
so far, so good, I have a data structure that I can address by column name 到目前为止,很好,我有一个数据结构,可以按列名进行寻址
print(data['name'][1]) #>>> Acquanegra
Next step, and question — I have a function that takes in input two vectors of geographical coordinates ( data['LON']
and data['LAT']
of course) and returns two arrays x
and y
of projected positions on a map (this works ok). 下一步,还有一个问题-我有一个函数,输入两个地理坐标向量(当然是
data['LON']
和data['LAT']
),并返回地图上投影位置的两个数组x
和y
(这样就可以了)。
I can live with separate vectors x
and y
but I'd like to augment data
with two new columns, data['x']
and data['y']
. 我可以使用单独的向量
x
和y
但是我想用两个新列data['x']
和data['y']
来扩充data
。 My naive attempt 我的天真尝试
data['x'], data['y'] = convert(data['LON'], data['LAT'])
raised a ValueError: no field of name x
, teaching me that data
has some traits of a dictionary but a dictionary is not. 引发了
ValueError: no field of name x
,告诉我data
具有字典的某些特征,但字典没有。
Is it possible to do as I want? 我可以做吗? tia
tia
Please consider that .hstack()
doesn't work with structured arrays aka record arrays, most previous answers work only for homogeneous arrays (the exception is mentioned in below comment by Warren). 请考虑
.hstack()
不适用于结构化数组或记录数组,大多数先前的答案仅适用于同类数组(沃伦在下面的注释中提到了例外)。
PS I'd prefer not to pandas
. PS我不想
pandas
。
You can use np.lib.recfunctions
: 您可以使用
np.lib.recfunctions
:
import numpy.lib.recfunctions as rfn
data = rfn.append_fields(data, ['x', 'y'], [x, y])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.