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 string 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])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.