Is it possible to add a new field in a numpy.genfromtxt output?

纵然是瞬间 提交于 2019-12-13 07:12:51

问题


I loaded a csv file and used the header to specify the names of each column.

# Load the Data
data = np.genfromtxt('dat_h.csv',
                     delimiter=',',
                     names=True)

This is great, because I can access the columns by their name. For example...

DATES = data['Dates']
Temperature = data['Temp']

Say I have a vector of pressure observations that matches these measurements. Can I append the data structure with a new field that includes my pressure variable?

I want to do something like this ...

data.append('Pressure',pressure_vector)
# and then be able to access that field like I do the other fields
data['Pressure']

回答1:


Look into this answer. Here are the docs for recfunctions.

Mainly I think what you need is this:

from numpy.lib.recfunctions import append_fields

append_fields(data, 'Pressure', pressure_vector, np.double)


来源:https://stackoverflow.com/questions/40182466/is-it-possible-to-add-a-new-field-in-a-numpy-genfromtxt-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!