numpy-将列数据类型(dtype)分配给现有数组 [英] Numpy - assign column data types (dtype) to existing array

查看:81
本文介绍了numpy-将列数据类型(dtype)分配给现有数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个给定的数组:

array = [(u'Andrew', -3, 3, 100.032) (u'Bob', -4, 4, 103.323) (u'Joe', -5, 5, 154.324)]

是从另一个处理CSV表的过程(我无法控制)生成的,并输出此numpy数组.我现在需要分配列的dtype进行进一步的分析.

that is generated from another process (that I cannot control) of taking a CSV table and it outputs this numpy array. I now need to assign the dtypes of the columns to do further analysis.

我该怎么做?

谢谢

推荐答案

这是您需要的吗?

new_array = np.array(array, dtype = [("name", object), 
                                     ("N1", int), 
                                     ("N2", int),
                                     ("N3", float)])

其中name和N1-3是我给的列名.

where name and N1-3 are column names I gave.

它给出了:

array([(u'Andrew', -3, 3, 100.032), (u'Bob', -4, 4, 103.323),
       (u'Joe', -5, 5, 154.324)], 
      dtype=[('name', 'O'), ('N1', '<i8'), ('N2', '<i8'), ('N3', '<f8')])

例如,您可以对"N1"进行排序:

You can sort on "N1" for instance :

new_array.sort(order="N1")
new_array
array([(u'Joe', -5, 5, 154.324), (u'Bob', -4, 4, 103.323),
       (u'Andrew', -3, 3, 100.032)], 
      dtype=[('name', 'O'), ('N1', '<i8'), ('N2', '<i8'), ('N3', '<f8')])

希望这会有所帮助.

这篇关于numpy-将列数据类型(dtype)分配给现有数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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