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

1 Answer 1

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)})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.