python 字符串数组转为整数数组

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]]

最新回复(0)
/jishumn5TGzUMT0V6UHApbVTYfKNKiWJbg0k43iZGumsCaKo_3D4858142
8 简首页