I have a numpy recarray with several integer columns and some string columns. The data in the string columns is composed 99% of integers, but numpy things it's a s开发者_Python百科tring because "NA" is in the column.
So I have two questions:
How do I remove the NA's and change them to 0s?
How can I convert the string columns to integers so that I can have a record array with many integer columns?
Thanks.
Use where
and astype
:
>>> x = np.array([123, 456, "789", "NA", "0", 0])
>>> x
array(['123', '456', '789', 'NA', '0', '0'], dtype='|S8')
>>> np.where(x != 'NA', x, 0).astype(int)
array([123, 456, 789, 0, 0, 0])
精彩评论