Skip to content Skip to sidebar Skip to footer

Genfromtxt - Force Column Name Generation For Unknown Number Of Columns

I have trouble getting numpy to load tabular data and automatically generate column names. It seems pretty simple but I cannot nail it. If i knew the number of columns I could easi

Solution 1:

I know there is a cleaner way to do this, but if you don't mind forcing the issue you can generate your dtype structure and assign it directly to the data array.

x = numpy.random.rand(10,10)
numpy.savetxt('test.out', x, delimiter=',')
dataa = numpy.genfromtxt('test.out',delimiter=",", dtype=None)
if dataa.dtype.names is None:#then dataa is homogenous?
  l1 = map(lambda z:('f%d'%(z),dataa.dtype),range(0,dataa.shape[1]))
  dataa.dtype = dtype(l1)

dataa.dtype
dataa.dtype.names

Post a Comment for "Genfromtxt - Force Column Name Generation For Unknown Number Of Columns"