0

Im trying to get a 10 x 8 array using the code below with numpy

import numpy as np
columns = ["Port Wt", "Bench Wt", "Port Retn", 
               "Bench Retn", "Attrib", "Select", "Inter", "Total"]
a = np.ones([10,len(columns)], 
               dtype={"names":columns, "formats":["f8"]*len(columns)})

I'm new to numpy and I get unexpected behaviour - I'm getting a 10 x 8 x 8 grid instead.

I've tried

a.dtype.names = columns

and get a ValueError: there are no fields defined

What am I doing wrong and how would I get a 10 x 8 grid as desired with the names?

Thanks

Tahnoon Pasha
  • 5,848
  • 14
  • 49
  • 75

1 Answers1

1

Your code does produce a 10 x 8 array, i.e. a.shape == (10, 8). However, each element in the array has 8 fields, adding to a total of 10 x 8 x 8 fields.

So what you probably want is an array with shape (10,) and 8 fields per element:

a = np.ones((10,), dtype={"names":columns, "formats":["f8"]*len(columns)})