【问题标题】:Is it possible to add a new field in a numpy.genfromtxt output?是否可以在 numpy.genfromtxt 输出中添加新字段?
【发布时间】:2016-10-21 17:36:41
【问题描述】:

我加载了一个 csv 文件并使用标题来指定每列的名称。

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

这太棒了,因为我可以通过名称访问列。比如……

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

假设我有一个与这些测量值相匹配的压力观测向量。我可以在数据结构中附加一个包含我的压力变量的新字段吗?

我想做这样的事情......

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

【问题讨论】:

    标签: python numpy genfromtxt


    【解决方案1】:

    查看this answer。这里是docs 的recfunctions。

    主要是我认为你需要的是:

    from numpy.lib.recfunctions import append_fields
    
    append_fields(data, 'Pressure', pressure_vector, np.double)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      • 2011-01-20
      • 1970-01-01
      相关资源
      最近更新 更多