我需要連接 2 個 rec.arrays(我在作業中對所有其他人執行的程序相同)。我的問題是我為陣列讀取的檔案之一,有 2 個額外的變數需要洗掉以匹配另一個陣列的變數以進行連接。我嘗試了幾件事,例如使用索引洗掉,都導致錯誤。
這是陣列
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', '<f8'), ('3co2_error', '<f8')
謝謝
uj5u.com熱心網友回復:
如果您從 csv 檔案加載這些陣列,那么使用usecols來選擇您加載的列可能是獲取兩個匹配的陣列的最簡單方法dtype。
但也可以從現有陣列中選擇欄位的子集。
為了顯示:
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})
這有一些復雜性,新 dtype 中view的引數證明了這一點。檔案頁面討論了這一點offsets。structured arrays有時需要copy使用該recfunctions.repack功能。
但它似乎view在使用時很好concatenate:
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索引中它們以不同的方式解釋,因此區別很重要。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/475083.html
