[Tutor] Convert structured 1D array to 2D array

Peter Otten __peter__ at web.de
Fri Feb 26 13:28:58 EST 2016


Ek Esawi wrote:

> I used genfromtxt to read a file with multiple data types. The result was
> a 1D array of tuples; for example
> 
> [(1, 2, 3), (‘a’, b’, ’c’), (‘12/12/2009’, ’2/4/2014’, ‘3/4/200)’]
> 
> 
> 
> I am trying to convert this structured array to a 2D array. Is this
> possible where the data types in the 1D array are preserved?

You can specify object as the dtype for a numpy array:

>>> t = [(1, 2, 3), ('a', 'b', 'c'), ('12/12/2009', '2/4/2014', '3/4/2000')]
>>> numpy.array(t, dtype=object)
array([[1, 2, 3],
       ['a', 'b', 'c'],
       ['12/12/2009', '2/4/2014', '3/4/2000']], dtype=object)




More information about the Tutor mailing list