我需要连接2个rec.array(与我在工作中对所有其他数组所做的步骤相同).我遇到的问题是,我为数组读取的一个文档中,有两个额外的变量需要删除,以匹配要连接的另一个数组的变量.我try 过几种方法,比如使用索引删除,都会导致错误.

这是数组

vswhr1
rec.array([('ny20110325s0a06c.001', 2011.23149798,  84.49677, 11.9223, 1.000e+00, 78.923, 11.923, 0.024, 0.024, 77.286, 189.465  ,  1.688, 180.     , 0.0019, 0., 0.00167, 60., 1003.84003, -15.7, 1003.84003, 65.8, -1., 0.    , -1., -1., 9.8765e+35, 9.8765e+35, 5.96541e+21, 2.60898e+19, 8.45080e+21, 7.92632e+19, 8.74633e+21, 8.68890e+19),
           ('ny20110325s0a06c.002', 2011.23150704,  84.50007, 12.0017, 2.000e+00, 78.923, 11.923, 0.024, 0.024, 77.325, 190.686  ,  1.694, 180.     , 0.0019, 0., 0.00167, 60., 1003.83002, -16. , 1003.83002, 68.7, -1., 0.    , -1., -1., 9.8765e+35, 9.8765e+35, 5.93553e+21, 2.54199e+19, 8.43518e+21, 7.75936e+19, 8.72990e+21, 8.60191e+19),
           ('ny20110325s0a06c.003', 2011.23150736,  84.50019, 12.0045, 3.000e+00, 78.923, 11.923, 0.024, 0.024, 77.326, 190.728  ,  1.694, 180.     , 0.0019, 0., 0.00167, 60., 1003.83002, -16.1, 1003.83002, 68.9, -1., 0.    , -1., -1., 9.8765e+35, 9.8765e+35, 5.93643e+21, 2.59443e+19, 8.42675e+21, 8.17653e+19, 8.73537e+21, 8.68880e+19),
           ...,
           ('ny20180919s0i06c.0042', 2018.71887239, 262.38843,  9.3221, 1.234e+03, 78.923, 11.923, 0.024, 0.027, 78.69 , 152.737  , -1.722, 180.00999, 0.0019, 0., 0.00188, 60., 1011.84003,  -2.2, 1011.84003, 77.6, -1., 0.0125, -1., -1., 9.8765e+35, 9.8765e+35, 2.11077e+22, 8.61874e+19, 8.72151e+21, 5.33405e+19, 9.01945e+21, 7.07619e+19),
           ('ny20180920s0i06c.0491', 2018.72160282, 263.38504,  9.2407, 1.235e+03, 78.923, 11.923, 0.024, 0.034, 79.177, 151.62399, -1.735, 180.00999, 0.0019, 0., 0.00188, 60., 1006.65997,   0. , 1006.65997, 62.8, -1., 0.0095, -1., -1., 9.8765e+35, 9.8765e+35, 1.96888e+22, 7.48627e+19, 8.70719e+21, 5.40175e+19, 8.97596e+21, 7.49834e+19),
           ('ny20180920s0i06c.0492', 2018.72161188, 263.38834,  9.3201, 1.236e+03, 78.923, 11.923, 0.024, 0.034, 79.072, 152.83299, -1.729, 180.00999, 0.0019, 0., 0.00188, 60., 1006.65997,  -0.6, 1006.65997, 64.6, -1., 0.0078, -1., -1., 9.8765e+35, 9.8765e+35, 1.94867e+22, 7.83111e+19, 8.71765e+21, 4.97304e+19, 8.97784e+21, 7.23055e+19)],
          dtype=[('spectrum', '<U21'), ('year', '<f8'), ('day', '<f8'), ('hour', '<f8'), ('run', '<f8'), ('lat', '<f8'), ('long', '<f8'), ('zobs', '<f8'), ('zmin', '<f8'), ('solzen', '<f8'), ('azim', '<f8'), ('osds', '<f8'), ('opd', '<f8'), ('fovi', '<f8'), ('amal', '<f8'), ('graw', '<f8'), ('tins', '<f8'), ('pins', '<f8'), ('tout', '<f8'), ('pout', '<f8'), ('hout', '<f8'), ('sia', '<f8'), ('fvsi', '<f8'), ('wspd', '<f8'), ('wdir', '<f8'), ('luft', '<f8'), ('luft_error', '<f8'), ('h2o', '<f8'), ('h2o_error', '<f8'), ('co2', '<f8'), ('co2_error', '<f8'), ('3co2', '<f8'), ('3co2_error', '<f8')])

vswhr1.shape 
(1236,) 

*无关数字

我需要删除las 2变量('3co2','&lt;f8'),('3co2_error','&lt;f8'))

非常感谢.

推荐答案

如果您是从csv文件加载这些数组,那么使用usecols Select 加载哪些列可能是获得dtype中匹配的两个数组的最简单方法.

但也可以从现有数组中 Select 字段子集.

为了说明:

In [1]: dt1 = np.dtype('U10,i,f')
In [2]: dt2 = np.dtype('U10,i,f,i,i')
In [3]: x = np.ones(2,dtype=dt1)
In [4]: y = np.zeros(2,dtype=dt2)
In [5]: x
Out[5]: 
array([('1', 1, 1.), ('1', 1, 1.)],
      dtype=[('f0', '<U10'), ('f1', '<i4'), ('f2', '<f4')])
In [6]: y
Out[6]: 
array([('', 0, 0., 0, 0), ('', 0, 0., 0, 0)],
      dtype=[('f0', '<U10'), ('f1', '<i4'), ('f2', '<f4'), ('f3', '<i4'), ('f4', '<i4')])

y个字段中的一个子集:

In [7]: y[['f0','f1','f2']]
Out[7]: 
array([('', 0, 0.), ('', 0, 0.)],
      dtype={'names': ['f0', 'f1', 'f2'], 'formats': ['<U10', '<i4', '<f4'], 'offsets': [0, 40, 44], 'itemsize': 56})

view中有一些复杂情况,新数据类型中的offsets参数就证明了这一点.structured arrays doc页面讨论了这一点.有时有必要使用recfunctions.repack函数生成copy.

但在concatenate中使用view似乎还可以:

In [8]: np.concatenate((x,y[['f0','f1','f2']]))
Out[8]: 
array([('1', 1, 1.), ('1', 1, 1.), ('', 0, 0.), ('', 0, 0.)],
      dtype={'names': ['f0', 'f1', 'f2'], 'formats': ['<U10', '<i4', '<f4'], 'offsets': [0, 40, 44], 'itemsize': 56})

我们还可以从另一个数组的dtype中获取索引列表:

In [9]: x.dtype.names
Out[9]: ('f0', 'f1', 'f2')

这是一个元组,我们需要将其转换为一个列表:

In [13]: np.concatenate((x,y[list(x.dtype.names)]))
Out[13]: 
array([('1', 1, 1.), ('1', 1, 1.), ('', 0, 0.), ('', 0, 0.)],
      dtype={'names': ['f0', 'f1', 'f2'], 'formats': ['<U10', '<i4', '<f4'], 'offsets': [0, 40, 44], 'itemsize': 56})

(在Python中,列表和元组通常是可互换的,但在numpy索引中,它们的解释方式不同,因此区别很重要.)

Python相关问答推荐

按列分区,按另一列排序

使可滚动框架在tkinter环境中看起来自然

Vectorize多个头寸的止盈/止盈回溯测试pythonpandas

图像 pyramid .难以创建所需的合成图像

如何制作10,000年及以后的日期时间对象?

如何从在虚拟Python环境中运行的脚本中运行需要宿主Python环境的Shell脚本?

所有列的滚动标准差,忽略NaN

部分视图的DataFrame

在Python中调用变量(特别是Tkinter)

如何检测鼠标/键盘的空闲时间,而不是其他输入设备?

如何在Python请求中组合多个适配器?

统计numpy. ndarray中的项目列表出现次数的最快方法

为用户输入的整数查找根/幂整数对的Python练习

如何在Python中将超链接添加到PDF中每个页面的顶部?

大型稀疏CSR二进制矩阵乘法结果中的错误

通过对列的其余部分进行采样,在Polars DataFrame中填充_null`?

多个布尔条件的`jax.lax.cond`等效项

判断字典键、值对是否满足用户定义的搜索条件

在Python Polar中从一个函数调用添加多个列

如何在保持sibling 姐妹美汤的同时插入和删除标签?