为什么numpy数组列数据类型没有更新? [英] Why is it that the numpy array column data type does not get updated?

查看:191
本文介绍了为什么numpy数组列数据类型没有更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

nd2values[:,[1]]=nd2values[:,[1]].astype(int)
nd2values

输出

array([['021fd159b55773fba8157e2090fe0fe2', '1',
        '881f83d2dee3f18c7d1751659406144e',
        '012059d397c0b7e5a30a5bb89c0b075e', 'A'],
       ['021fd159b55773fba8157e2090fe0fe2', '1',
        'cec898a1d355dbfbad8c760615fde1af',
        '012059d397c0b7e5a30a5bb89c0b075e', 'A'],
       ['021fd159b55773fba8157e2090fe0fe2', '1',
        'a99f44bbff39e352191a870e17f04537',
        '012059d397c0b7e5a30a5bb89c0b075e', 'A'],
       ...,
       ['fdeb2950c4d5209d449ebd2d6afac11e', '4',
        '4f4e47023263931e1445dc97f7dae941',
        '3cd0b15957ceb80f5125bef8bd1bbea7', 'A'],
       ['fdeb2950c4d5209d449ebd2d6afac11e', '4',
        '021dabc5d7a1404ec8ad34fe8ca4b5e3',
        '3cd0b15957ceb80f5125bef8bd1bbea7', 'A'],
       ['fdeb2950c4d5209d449ebd2d6afac11e', '4',
        'f79a2b5e6190ac3c534645e806f1b611',
        '3cd0b15957ceb80f5125bef8bd1bbea7', 'A']], dtype='<U32')

第二列的数据类型仍为 str 。是因为这个特殊的numpy数组有 dtype 限制吗?您如何将第二列更改为 int ?谢谢。

The data type of the second column is still str. Is it because this particular numpy array has dtype restriction? How would you change the second column to int? Thanks.

np.array(nd2values,dtype=[str,int,str,str,str])

给出

TypeError: data type not understood


推荐答案

分配正在施放你的int到数组的类型。为了能够在数组中保存所有类型的对象,将dtype设置为object。

The assignement is casting your ints to the type of the array. To be able to hold all kind of objects in an array set the dtype to object.

nd2values = nd2values.astype(object)

然后

nd2values[:,[1]]=nd2values[:,[1]].astype(int)

这篇关于为什么numpy数组列数据类型没有更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆