【发布时间】:2017-04-01 07:06:38
【问题描述】:
我有一个导出的 pandas 数据框,它现在是一个 numpy.array 对象。
subset = array[:4,:]
array([[ 2. , 12. , 33.33333333, 2. ,
33.33333333, 12. ],
[ 2. , 2. , 33.33333333, 2. ,
33.33333333, 2. ],
[ 2.8 , 8. , 45.83333333, 2.75 ,
46.66666667, 13. ],
[ 3.11320755, 75. , 56. , 3.24 ,
52.83018868, 33. ]])
print subset.dtype
dtype('float64')
我要将列值转换为特定类型,并设置列名,这意味着我需要将其转换为 ndarray。
这是我的数据类型:
[('PERCENT_A_NEW', '<f8'), ('JoinField', '<i4'), ('NULL_COUNT_B', '<f8'),
('PERCENT_COMP_B', '<f8'), ('RANKING_A', '<f8'), ('RANKING_B', '<f8'),
('NULL_COUNT_B', '<f8')]
当我去转换数组时,我得到:
ValueError: new type not compatible with array.
如何将每列转换为特定值,以便我可以将数组转换为 ndarray?
谢谢
【问题讨论】:
-
您应该使用正确的数据类型,例如
np.int16、np.float32、np.float64.... -
您可以使用
.astype方法在 pandas 本身中执行此操作。为什么不必要地转换为数组? -
@Kartik 我正在使用的程序使用 numpy 数组。
标签: python python-2.7 pandas numpy