Converting a 2D numpy array to a structured array

前端 未结 5 1685
故里飘歌
故里飘歌 2020-11-29 01:08

I\'m trying to convert a two-dimensional array into a structured array with named fields. I want each row in the 2D array to be a new record in the structured array. Unfortu

5条回答
  •  醉话见心
    2020-11-29 01:53

    If the data starts as a list of tuples, then creating a structured array is straight forward:

    In [228]: alist = [("Hello",2.5,3),("World",3.6,2)]
    In [229]: dt = [("Col1","S8"),("Col2","f8"),("Col3","i8")]
    In [230]: np.array(alist, dtype=dt)
    Out[230]: 
    array([(b'Hello',  2.5, 3), (b'World',  3.6, 2)], 
          dtype=[('Col1', 'S8'), ('Col2', '

    The complication here is that the list of tuples has been turned into a 2d string array:

    In [231]: arr = np.array(alist)
    In [232]: arr
    Out[232]: 
    array([['Hello', '2.5', '3'],
           ['World', '3.6', '2']], 
          dtype='

    We could use the well known zip* approach to 'transposing' this array - actually we want a double transpose:

    In [234]: list(zip(*arr.T))
    Out[234]: [('Hello', '2.5', '3'), ('World', '3.6', '2')]
    

    zip has conveniently given us a list of tuples. Now we can recreate the array with desired dtype:

    In [235]: np.array(_, dtype=dt)
    Out[235]: 
    array([(b'Hello',  2.5, 3), (b'World',  3.6, 2)], 
          dtype=[('Col1', 'S8'), ('Col2', '

    The accepted answer uses fromarrays:

    In [236]: np.rec.fromarrays(arr.T, dtype=dt)
    Out[236]: 
    rec.array([(b'Hello',  2.5, 3), (b'World',  3.6, 2)], 
              dtype=[('Col1', 'S8'), ('Col2', '

    Internally, fromarrays takes a common recfunctions approach: create target array, and copy values by field name. Effectively it does:

    In [237]: newarr = np.empty(arr.shape[0], dtype=dt)
    In [238]: for n, v in zip(newarr.dtype.names, arr.T):
         ...:     newarr[n] = v
         ...:     
    In [239]: newarr
    Out[239]: 
    array([(b'Hello',  2.5, 3), (b'World',  3.6, 2)], 
          dtype=[('Col1', 'S8'), ('Col2', '

提交回复
热议问题