转换numpy数组中特定列的dtype [英] Convert dtype of a specific column in a numpy array
本文介绍了转换numpy数组中特定列的dtype的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想更改numpy列的数据类型,但是当我替换原始的numpy列时,dtype将不会成功更改.
I want change the numpy column data type, but when I to replace the original numpy column, the dtype will not change succesfully.
import numpy as np
arraylist =[(1526869384.273246, 0, 'a0'),
(1526869385.273246, 1, 'a1'),
(1526869386.273246, 2, 'a2'),
(1526869387.273246, 3, 'a3'),
(1526869388.273246, 4, 'a4'),
(1526869389.273246, 5, 'a5'),
(1526869390.273246, 6, 'a6'),
(1526869391.273246, 7, 'a7'),
(1526869392.273246, 8, 'a8'),
(1526869393.273246, 9, 'a9'),
(1526869384.273246, 0, 'a0'),
(1526869385.273246, 1, 'a1'),
(1526869386.273246, 2, 'a2'),
(1526869387.273246, 3, 'a3'),
(1526869388.273246, 4, 'a4'),
(1526869389.273246, 5, 'a5'),
(1526869390.273246, 6, 'a6'),
(1526869391.273246, 7, 'a7'),
(1526869392.273246, 8, 'a8'),
(1526869393.273246, 9, 'a9')]
array = np.array(arraylist)
array.dtype
dtype('<U32')
array[:,0]=array[:,0].astype("float64")
array[:,0].dtype
>>> dtype('<U32')
通过事件我更改了列的dtype,但是为什么我要替换原始列仍为u32
?
Event through I changed the dtype of the column, but why I want to replace the orignal column it's still u32
?
推荐答案
如果可以使用命名列,则可以定义dtypes的元组,并在创建过程中将它们分配给array
:
If you're okay with named columns, you can define a tuple of dtypes and assign them to array
during creation:
dtype = [('A', 'float'), ('B', 'int'), ('C', '<U32')]
array = np.array(arraylist, dtype=dtype)
array['A'].dtype # note, array[: 0] does not work here since these are named columns
dtype('float64')
这篇关于转换numpy数组中特定列的dtype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文