python 字符串数组转为整数数组,将numpy字符串数组转换为int数组

I have a numpy.ndarray

a = [['-0.99' '' '0.56' ..., '0.56' '-2.02' '-0.96']]

how to convert it to int?

output :

a = [[-0.99 0.0 0.56 ..., 0.56 -2.02 -0.96]]

I want 0.0 in place of blank ''

解决方案import numpy as np

a = np.array([['-0.99', '', '0.56', '0.56', '-2.02', '-0.96']])

a[a == ''] = 0.0

a = a.astype(np.float)

Result is:

[[-0.99 0. 0.56 0.56 -2.02 -0.96]]

Your values are floats, not integers. It is not clear if you want a list of lists or a numpy array as your end result. You can easily get a list of lists like this:

a = a.tolist()

Result:

[[-0.99, 0.0, 0.56, 0.56, -2.02, -0.96]]

你可能感兴趣的:(python,字符串数组转为整数数组)