在ndarray中插入使用numpy.genfromtxt创建的列

时间:2016-11-30 15:38:04

标签: python arrays numpy numpy-dtype

我使用numpy.genfromtxt从txt文件创建了ndarray。 txt文件如下所示:

2505000
10.316     1.936     0.025    0   15   0   0
10.316     1.937     0.028    0   15   0   0
10.316     1.943     0.028    0   15   0   0
10.316     1.954     0.022    0    9  58   0
10.316     1.955     0.026    0    9  58   0

我正在运行读取数据:

data = np.genfromtxt(fname,
                     skip_header =1,
                     dtype = [('X','f'),('Y','f'),('Z','f'),('V', 'i'),('T', 'i')],
                     usecols = (0,1,2,4,5))

我现在正在尝试复制第4列(我称之为V)并将其置于其后(在最后T列之前)。

我知道我可以通过data['V']复制该列,但如何将此副本放在VT(第4和第5列)之间?我查看了numpy.insert,但我不确定如何指定索引(obj参数)。我尝试numpy.insert(data_copy, data_copy['T'],data_copy['V'])没有运气(我收到了IndexError: index 61 is out of bounds for size 20)。

1 个答案:

答案 0 :(得分:1)

您必须使用添加的字段创建新的FlexibleContexts,并按字段名称将值从原始数组复制到新数组。

dtype具有执行此操作的功能(以及相关操作):

recfunctions

numpy dtype error - (structured array creation)

相关问题