Reputation: 13
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]])
Upvotes: 1
Views: 880
Reputation: 81
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.]])
Upvotes: 1
Reputation: 19430
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.]]
Upvotes: 1