1

My array is 2D numpystr352 and contains a mix of numbers and strings. I want to convert it to float numpy so that the string elements should convert to nan (without using Pandas).

For example:

import numpy as np

x = np.array([[1, 2, 'tom'], [4, 'Manu', 6]])

What I want is:

x = np.array([[1, 2, nan], [4, nan, 6]])
2

2 Answers 2

1

Using genfromtext as suggested here is not possible with multi-dimensional arrays. On the other hand, you can apply genfromtext to each row of the 2D array using apply_along_axis as described here:

import numpy as np

x = np.array([[1, 2, 'tom'], [4, 'Manu', 6]])
print(np.apply_along_axis(np.genfromtxt, 1 ,x))

Which will give:

[[ 1.  2. nan]
 [ 4. nan  6.]]
Sign up to request clarification or add additional context in comments.

Comments

1

My solution iterates over the flattened array and checks if each type can be float. If not I will fill in a np.NaN instead. After this I will recreate the numpy array with dtype float into the old shape:

import numpy as np

x = np.array([[1, 2, 'tom'], [4, 'Manu', 6]])

shape = x.shape
x_nan = np.empty_like(x).flatten()
for i,x in enumerate(x.flat):
    try:
        x_nan[i] = float(x)
    except:
        x_nan[i] = np.NaN

x_nan = np.array(x_nan, dtype=np.float).reshape(shape)
print(x_nan)
# array([[ 1.,  2., nan],
#        [ 4., nan,  6.]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.