0

I have data which is a list of 5-tuples. The first two are integer indices i, j and the next three are floats xyz.

data = [(1, 2, 3.141, 1.414, 2.718), 
        (3, 4, 1.111, 2.222, 3.333),
        (0, 0, 0.000, 0.000, 0.000)]

I have heard that I can do something like

dt    = [('ij', 'int', 2), ('xyz', 'float', 3)]

struct_array = np.array(data, dtype=dt)

so I can access the last three columns of the array as a 2D float array. For example to get r = sqrt(x^2 + y^2 + z^2) I should be able to say

r = np.sqrt(((struct_array['xyz']**2).sum(axis=1)))

and get the result

array([4.38780139, 4.15698136, 0.        ])

the same way that

normal_array = np.array(data)

r = np.sqrt(((array[:, 2:]**2).sum(axis=1)))

but everything I've tried results in the error message

ValueError: could not assign tuple of length 5 to structure with 2 fields.

I've looked at https://docs.scipy.org/doc/numpy/user/basics.rec.html but if the answer to why my attempt fails is there I'm not seeing it.

1 Answer 1

1

You'll have to pack the data into 2 tuples for the two elements of your structure:

struct_array = np.array([((e[0],e[1]), (e[2],e[3],e[4])) for e in data], dtype=dt)
np.sqrt(((struct_array['xyz']**2).sum(axis=1)))

Result

array([4.38780139, 4.15698136, 0.        ])
Sign up to request clarification or add additional context in comments.

3 Comments

Problem solved, excellent! This is exactly what I need, I can put off learning pandas for another year.
Glad I could help. As a bonus, here's the equivalent pandas solution just for information: df = pd.DataFrame(data, columns=list('ijxyz')) np.sqrt((df[['x','y','z']]**2).sum(axis=1)). But the pure numpy solution is of course easier in this case.
okay since I've already gotten my feet wet I'll script it both ways for a while and see which one looks like a better way to go long term. Thanks!

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.