Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何拆分混合的dtype 1D numpy数组?_Arrays_Numpy_Split_Mixed_Genfromtxt - Fatal编程技术网

Arrays 如何拆分混合的dtype 1D numpy数组?

Arrays 如何拆分混合的dtype 1D numpy数组?,arrays,numpy,split,mixed,genfromtxt,Arrays,Numpy,Split,Mixed,Genfromtxt,也许这个问题以前已经得到了回答,但我很难找到答案。假设文件中包含以下数据: date, id, int1, int2, int3 02/03/2015, 2, 23, 65, 99 10/06/2016, 4, 84, 12, 35 10/01/2017, 6, 53, 6, 78 我可以用以下格式快速编写numpy代码片段: import StringIO import numpy as np hdr = 'date, id, int1, int2, int3' date = ''' 02

也许这个问题以前已经得到了回答,但我很难找到答案。假设文件中包含以下数据:

date, id, int1, int2, int3
02/03/2015, 2, 23, 65, 99
10/06/2016, 4, 84, 12, 35
10/01/2017, 6, 53, 6, 78
我可以用以下格式快速编写numpy代码片段:

import StringIO
import numpy as np

hdr = 'date, id, int1, int2, int3'
date = '''
02/03/2015, 2, 23, 65, 99
10/06/2016, 4, 84, 12, 35
10/01/2017, 6, 53, 6, 78
'''
lines = '%s%s' % (hdr, date)
pseudo_file = StringIO.StringIO(lines)
np_dtypes = 'S10,%s' % ','.join(['i4' for x in hdr.split(',')[1:]])

np1 = np.genfromtxt(pseudo_file, delimiter=',', names=True, dtype=np_dtypes)

print np1
print np1.dtype.names
print np1.shape
print np1['date']
print np1['int3']
这将为我提供以下输出:

[('02/03/2015', 2, 23, 65, 99) ('10/06/2016', 4, 84, 12, 35)
 ('10/01/2017', 6, 53, 6, 78)]
('date', 'id', 'int1', 'int2', 'int3')
(3L,)
['02/03/2015' '10/06/2016' '10/01/2017']
[99 35 78]
可以看出,numpy能够成功解析数组。但是,如何将其分为两部分:

  • 仅包含字符串的一维数组(日期列)
  • 另一个仅包含整数的一维数组

  • 拆分应以保持每列名称结构的方式进行。

    您是否已使用
    np1['date']
    拆分字符串?要保留其列名,可以将“日期”列名放入列表中(谢谢@hpaulj):

    要获取ints,请执行以下操作:

    ints=np1[['int1','int2','int3']]
    ints
    #array([(23, 65, 99), (84, 12, 35), (53, 6, 78)], 
    #      dtype=[('int1', '<i4'), ('int2', '<i4'), ('int3', '<i4')])
    
    ints=np1[['int1','int2','int3']
    ints
    #数组([(23,65,99),(84,12,35),(53,6,78)],
    
    #dtype=[('int1','您是否已经用
    np1['date']
    拆分了字符串?要保留其列名,您可以将'date'列名放在列表中(谢谢@hpaulj):

    要获取ints,请执行以下操作:

    ints=np1[['int1','int2','int3']]
    ints
    #array([(23, 65, 99), (84, 12, 35), (53, 6, 78)], 
    #      dtype=[('int1', '<i4'), ('int2', '<i4'), ('int3', '<i4')])
    
    ints=np1[['int1','int2','int3']
    ints
    #数组([(23,65,99),(84,12,35),(53,6,78)],
    
    #dtype=[('int1','
    dates=np1[['date']]]
    应该适用于日期列,就像'int'列表一样。谢谢@tom。我缺乏非常基本的numpy用法(现在仍然如此)。我会花很多时间考虑这个解决方案。主要是因为我在搜索numpy方法。lol.;-)干杯。@hpaulj没有保留问题所要求的列的名称-这是一个同时访问多个字段的问题。如果列表中有
    'date'
    ,如您的int大小写,将返回完整的数据类型和名称。无论列表中有一项还是三项,该列表索引都有效。
    dates=np1['date']
    应该适用于日期列,就像“int”列表一样。谢谢@tom。我缺乏非常基本的numpy用法(现在仍然如此)。我会花很多时间考虑这个解决方案。主要是因为我在搜索numpy方法。lol.;-)干杯。@hpaulj没有保留问题所要求的列的名称-这是一个同时访问多个字段的问题。如果列表中有
    'date'
    ,如int大小写,则将返回完整的数据类型和名称。该列表索引无论列表中有一项还是三项都有效。