I have a dictionary that I need to convert to a NumPy structured array. I'm using the arcpy function NumPyArraytoTable
, so a NumPy structured array is the only data format that will work.
Based on this thread: Writing to numpy array from dictionary and this thread: How to convert Python dictionary object to numpy array
I've tried this:
result = {0: 1.1181753789488595, 1: 0.5566080288678394, 2: 0.4718269778030734, 3: 0.48716683119447185, 4: 1.0, 5: 0.1395076201641266, 6: 0.20941558441558442}
names = ['id','data']
formats = ['f8','f8']
dtype = dict(names = names, formats=formats)
array=numpy.array([[key,val] for (key,val) in result.iteritems()],dtype)
But I keep getting expected a readable buffer object
The method below works, but is stupid and obviously won't work for real data. I know there is a more graceful approach, I just can't figure it out.
totable = numpy.array([[key,val] for (key,val) in result.iteritems()])
array=numpy.array([(totable[0,0],totable[0,1]),(totable[1,0],totable[1,1])],dtype)