我只想从一个numpy数组中获取dtype的列表或dict。认为这将是一件容易的事,但它无法实现。我看了其他地方但找不到答案。 jn是一个重新组合
[OrderedDict(row) for i, row in jn.iterrows()]
jn.index.dtype
dtype('object')
jn.to_records()
#this put out record dtype
jn.to_records().dtype
dtype([('index', '<i8'), ('scalerank', 'O'), ('featurecla', 'O'), ('labelrank', 'O'), ('sovereignt', 'O'), ('sov_a3', 'O'), ('adm0_dif', 'O'), ('level', 'O'), ('type', 'O'), ('admin', 'O'), ('adm0_a3', 'O'), ('geou_dif', 'O'), ('geounit', 'O'), ('gu_a3', 'O'), ('su_dif', 'O'), ('subunit', 'O'), ('su_a3', 'O'), ('brk_diff', 'O'), ('name', 'O'), ('name_long', 'O'), ('brk_a3', 'O'), ('brk_name', 'O'), ('brk_group', 'O'), ('abbrev', 'O'), ('postal', 'O'), ('formal_en', 'O'), ('formal_fr', 'O'), ('note_adm0', 'O'), ('note_brk', 'O'), ('name_sort', 'O'), ('name_alt', 'O'), ('mapcolor7', 'O'), ('mapcolor8', 'O'), ('mapcolor9', 'O'), ('mapcolor13', 'O'), ('pop_est', 'O'), ('gdp_md_est', 'O'), ('pop_year', 'O'), ('lastcensus', 'O'), ('gdp_year', 'O'), ('economy', 'O'), ('income_grp', 'O'), ('wikipedia', 'O'), ('fips_10', 'O'), ('iso_a2', 'O'), ('iso_a3', 'O'), ('iso_n3', '<i4'), ('un_a3', 'O'), ('wb_a2', 'O'), ('wb_a3', 'O'), ('woe_id', 'O'), ('adm0_a3_is', 'O'), ('adm0_a3_us', 'O'), ('adm0_a3_un', 'O'), ('adm0_a3_wb', 'O'), ('continent', 'O'), ('region_un', 'O'), ('subregion', 'O'), ('region_wb', 'O'), ('name_len', 'O'), ('long_len', 'O'), ('abbrev_len', 'O'), ('tiny', 'O'), ('homepart', 'O'), ('Country', 'O'), ('Index', '<i8'), ('A2', 'O'), ('A3', 'O'), ('Code', '<i4'), ('1', '<f8'), ('730', '<f8'), ('1000', '<f8'), ('1150', '<f8'), ('1280', '<f8'), ('1300', '<f8'), ('1348', '<f8'), ('1400', '<f8'), ('1450', '<f8'), ('1500', '<f8'), ('1550', '<f8'), ('1570', '<f8'), ('1600', '<f8'), ('1650', '<f8'), ('1700', '<f8'), ('1720', '<f8'), ('1750', '<f8'), ('1775', '<f8'), ('1800', '<f8'), ('1820', '<f8'), ('1850', '<f8'), ('1870', '<f8'), ('1890', '<f8'), ('1913', '<f8'), ('1929', '<f8'), ('1950', '<f8'), ('1960', '<f8'), ('1973', '<f8'), ('1980', '<f8'), ('1990', '<f8'), ('2000', '<f8'), ('2008', '<f8')])
kd = jn.to_records().dtype
print kd
gsc = []
from itertools import chain
gsc = list(chain(kd))
error
5 gsc = list(chain(kd))
TypeError:'numpy.dtype'对象不可迭代
答案 0 :(得分:4)
结构化数组的dtype的fields
属性就像一个字典。字段名称是键,值是保存字段类型和偏移量的元组。
例如:
In [22]: a
Out[22]:
array([(1.0, 2.0, 99), (3.0, 4.0, 75)],
dtype=[('x', '<f4'), ('y', '<f4'), ('code', '<i8')])
In [23]: a.dtype.fields
Out[23]:
<dictproxy {'code': (dtype('int64'), 8),
'x': (dtype('float32'), 0),
'y': (dtype('float32'), 4)}>
In [24]: for name, typ in a.dtype.fields.items():
....: print "%-12s %-20s %d" % (name, typ[0], typ[1])
....:
y float32 4
x float32 0
code int64 8
答案 1 :(得分:3)
回答OP特定问题:
字典(未排序):
{x:str(y[0]) for x,y in yourArray.dtype.fields.items()}
列表(按列排序):
[(x,str(y[0])) for x,y in sorted(yourArray.dtype.fields.items(),key=lambda k: k[1])]
非常感谢Warren Weckesser对dtype.fields的回答是我第一次来到这里:)