Question

I just want to get a list or dict from dtype out of a numpy array. Thought it would be a easy but it is not itterable. I looked other places but could not find answer. jn is a recarray

[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' object is not iterable

Was it helpful?

Solution

The fields attribute of the dtype of a structured array acts like a dictionary. The field names are the keys, and the values are tuples holding the field's type and offset.

For example:

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

OTHER TIPS

To answer the OPs specific question:

Dictionary (unsorted):

{x:str(y[0]) for x,y in yourArray.dtype.fields.items()}

List (sorted by column):

[(x,str(y[0])) for x,y in sorted(yourArray.dtype.fields.items(),key=lambda k: k[1])]

Big thanks to Warren Weckesser who's answer of the dtype.fields is how I came to the above in the first place :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top