K.S.
K.S.

Reputation: 15

Convert 1D strings array to a 2D float array

I am trying to convert the data type of a list into float. I know how to convert the data type of each list using for-loop, however, I really don't know how to convert the data type of each item of a list, i.e., I have an array with the data type string such that

array(['5, 0, -150, 0', '6, 0, -10, 0',
       '7, 2.5881904510252, 9.6592582628907, 0',
       '8, 5, 8.6602540378444, 0',
       '9, 7.0710678118655, 7.0710678118655, 0',
       '10, 8.6602540378444, 5, 0'], dtype='<U63')

then, how can I build two dimensional array as 6x4 array of float data type?

Upvotes: 0

Views: 413

Answers (2)

hpaulj
hpaulj

Reputation: 231665

In [72]: arr = np.array(['5, 0, -150, 0', '6, 0, -10, 0', 
    ...:        '7, 2.5881904510252, 9.6592582628907, 0', 
    ...:        '8, 5, 8.6602540378444, 0', 
    ...:        '9, 7.0710678118655, 7.0710678118655, 0', 
    ...:        '10, 8.6602540378444, 5, 0'], dtype='<U63')                     

The list comprehension that the others propose is the right way, but it can be simplified:

In [73]: [line.split(',') for line in arr]                                      
Out[73]: 
[['5', ' 0', ' -150', ' 0'],
 ['6', ' 0', ' -10', ' 0'],
 ['7', ' 2.5881904510252', ' 9.6592582628907', ' 0'],
 ['8', ' 5', ' 8.6602540378444', ' 0'],
 ['9', ' 7.0710678118655', ' 7.0710678118655', ' 0'],
 ['10', ' 8.6602540378444', ' 5', ' 0']]

np.array can take care of handling the nested lists, and conversion to float:

In [74]: np.array(_, dtype=float)                                                     
Out[74]: 
array([[   5.        ,    0.        , -150.        ,    0.        ],
       [   6.        ,    0.        ,  -10.        ,    0.        ],
       [   7.        ,    2.58819045,    9.65925826,    0.        ],
       [   8.        ,    5.        ,    8.66025404,    0.        ],
       [   9.        ,    7.07106781,    7.07106781,    0.        ],
       [  10.        ,    8.66025404,    5.        ,    0.        ]])

The fact that the original object is an array rather than a list doesn't enhance this conversion. In fact iterating on the array is slower than iterating on the equivalent list.

Upvotes: 0

Vicrobot
Vicrobot

Reputation: 3988

Iterate on that array, split those strings on delimiter, then make them of float datatype.

>>> arr2 = np.array([np.array([float(i.strip()) for i in j.split(',') if i]) for j in arr1])
>>> arr2
array([[   5.        ,    0.        , -150.        ,    0.        ],
       [   6.        ,    0.        ,  -10.        ,    0.        ],
       [   7.        ,    2.58819045,    9.65925826,    0.        ],
       [   8.        ,    5.        ,    8.66025404,    0.        ],
       [   9.        ,    7.07106781,    7.07106781,    0.        ],
       [  10.        ,    8.66025404,    5.        ,    0.        ]])
>>> arr2.dtype
dtype('float64')

Upvotes: 3

Related Questions