我只想从 numpy 数组中的 dtype 获取列表或字典。以为这会很容易,但其实并不难。我查看了其他地方但找不到答案。 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))
类型错误:'numpy.dtype'对象不可迭代
fields
属性就像字典一样。 字段名称是键,值是保存字段类型和偏移量的元组。
例如:
In [22]: a
Out[22]:
array([(1., 2., 99), (3., 4., 75)],
dtype=[('x', '<f4'), ('y', '<f4'), ('code', '<i8')])
In [23]: a.dtype.fields
Out[23]:
mappingproxy({'x': (dtype('float32'), 0),
'y': (dtype('float32'), 4),
'code': (dtype('int64'), 8)})
In [24]: for name, typ in a.dtype.fields.items():
....: print("%-12s %-20s %d" % (name, typ[0], typ[1]))
....:
y float32 0
x float32 4
code int64 8
回答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 的回答是我最初想到上述问题的原因:)
要获取词典,您只需使用:
dtype.fields.copy()
列表可以通过已经提到的列表推导式之一创建。