1

我需要将格式化的字符串放入结构化数组(字符串是 JSON 格式的 2D 表,所有列都是objects)。现在,我这样做:

import json
import numpy
json_string  = '{"SYM": ["this_string","this_string","this_string"],"DATE": ["NaN","NaN","NaN"],"YEST": ["NaN","NaN","NaN"],"other_DATE": ["NaN","NaN","NaN"],"SIZE": ["NaN","NaN","NaN"],"ACTIVITY": ["2019-09-27 14:18:28.000700 UTC","2019-09-27 14:18:28.000700 UTC","2019-09-27 14:18:28.000600 UTC"]}'
all_content  = json.loads(json_string)
dtype        = numpy.dtype(dict(names = list(all_content.keys()), formats = ['O'] * len(all_content.keys())))
this_bucket  = numpy.empty(shape = [len(all_content[next(iter(all_content.keys()))]), ], 
                                dtype = dtype)
for key in all_content.keys():
    this_bucket[key][:] = all_content[key]

但这似乎非常冗长。有直接的方法吗?

4

1 回答 1

1

基本上有两种方法可以设置结构化数组的值 - 逐个字段分配值(您可以这样做),以及使用元组列表,我将对此进行演示:

In [180]: all_content                                                           
Out[180]: 
{'SYM': ['this_string', 'this_string', 'this_string'],
 'DATE': ['NaN', 'NaN', 'NaN'],
 'YEST': ['NaN', 'NaN', 'NaN'],
 'other_DATE': ['NaN', 'NaN', 'NaN'],
 'SIZE': ['NaN', 'NaN', 'NaN'],
 'ACTIVITY': ['2019-09-27 14:18:28.000700 UTC',
  '2019-09-27 14:18:28.000700 UTC',
  '2019-09-27 14:18:28.000600 UTC']}

制作一个对象 dtype 数组,主要是为了“列”索引方便。

In [181]: arr = np.array(list(all_content.items()))                             
In [182]: arr                                                                   
Out[182]: 
array([['SYM', list(['this_string', 'this_string', 'this_string'])],
       ['DATE', list(['NaN', 'NaN', 'NaN'])],
       ['YEST', list(['NaN', 'NaN', 'NaN'])],
       ['other_DATE', list(['NaN', 'NaN', 'NaN'])],
       ['SIZE', list(['NaN', 'NaN', 'NaN'])],
       ['ACTIVITY',
        list(['2019-09-27 14:18:28.000700 UTC', '2019-09-27 14:18:28.000700 UTC', '2019-09-27 14:18:28.000600 UTC'])]],
      dtype=object)

定义 dtype - 正如你所做的那样,或者使用:

In [183]: dt = np.dtype(list(zip(arr[:,0],['O']*arr.shape[0])))                 
In [184]: dt                                                                    
Out[184]: dtype([('SYM', 'O'), ('DATE', 'O'), ('YEST', 'O'), ('other_DATE', 'O'), ('SIZE', 'O'), ('ACTIVITY', 'O')])

List 'transpose' 产生一个元组列表:

In [185]: list(zip(*arr[:,1]))                                                  
Out[185]: 
[('this_string', 'NaN', 'NaN', 'NaN', 'NaN', '2019-09-27 14:18:28.000700 UTC'),
 ('this_string', 'NaN', 'NaN', 'NaN', 'NaN', '2019-09-27 14:18:28.000700 UTC'),
 ('this_string', 'NaN', 'NaN', 'NaN', 'NaN', '2019-09-27 14:18:28.000600 UTC')]

此列表适合作为数据输入:

In [186]: np.array(list(zip(*arr[:,1])),dtype=dt)                               
Out[186]: 
array([('this_string', 'NaN', 'NaN', 'NaN', 'NaN', '2019-09-27 14:18:28.000700 UTC'),
       ('this_string', 'NaN', 'NaN', 'NaN', 'NaN', '2019-09-27 14:18:28.000700 UTC'),
       ('this_string', 'NaN', 'NaN', 'NaN', 'NaN', '2019-09-27 14:18:28.000600 UTC')],
      dtype=[('SYM', 'O'), ('DATE', 'O'), ('YEST', 'O'), ('other_DATE', 'O'), ('SIZE', 'O'), ('ACTIVITY', 'O')])

您可以通过以下方式简化获取键/字段的数量:

In [187]: len(all_content)                                                      
Out[187]: 6

获取“记录”数量的另一种方法是

In [188]: first,*rest=all_content.values()                                      
In [189]: first                                                                 
Out[189]: ['this_string', 'this_string', 'this_string']

next(iter...)的可能一样好。

于 2019-10-08T01:21:00.610 回答