开发者

Data type conversion of column in numpy record array

开发者 https://www.devze.com 2023-04-06 22:00 出处:网络
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\

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

精彩评论

暂无评论...
验证码 换一张
取 消