Reputation: 25023
I have the following data in geo.dat
id lon lat inhab name
1 9. 45. 100 Ciriè
2 10. 45. 60 Acquanegra
and I get it in a 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).
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']
. 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.
Is it possible to do as I want? 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).
PS I'd prefer not to pandas
.
Upvotes: 0
Views: 2819
Reputation: 97565
You can use np.lib.recfunctions
:
import numpy.lib.recfunctions as rfn
data = rfn.append_fields(data, ['x', 'y'], [x, y])
Upvotes: 5