# How to make a numpy ndarray from several numpy arrays

I am not to figure out how to form a ndarray from existing numpy arrays. Suppose I have three arrays - index, distA, and distB. Now the I want to form a ndarray of these three arrays

What I have tried -

>>> indices = np.array([[5,7,4], [6,4,8]])
>>> distances = np.array([[0.2, 0.3, 0.4], [0.3, 0.4, 0.5]])
>>> np.column_stack((indices, distances))
array([[5. , 7. , 4. , 0.2, 0.3, 0.4],
[6. , 4. , 8. , 0.3, 0.4, 0.5]])


But I want to form a table-like structure so that I can retrieve values in distA and distB.

5, 0.2, 0
7, 0.3, 0
4, 0.4, 0.4
6, 0, 0.3
8, 0, 0.5


Is it possible to do this with numpy?

• I can't see how you aim to obtain a 5x3 matrix from two 2x3 arrays
– yatu
Dec 11, 2018 at 8:55
• @nixon Neither me, I was thinking there would be some to accomplish this. Dec 11, 2018 at 9:02
• Its just that I can't see the logic on how to build this ndarray, can you better explain please?
– yatu
Dec 11, 2018 at 9:15
• There would not be a direct way to do this, but if we could do a union of indices and find the corresponding distance in a and in b, then using pandas dataframe we can build this. Not sure if this a good question to ask, still a noob. Dec 11, 2018 at 9:24

If your purpose is to deal with some data analysis from your arrays and have a nice view of it, try this using pandas:

import pandas as pd

df = pd.DataFrame({'distA' : np.array([0.2, 0.3, 0.4]),
'distB' : np.array([0.3, 0.4, 0.5])})

df


In the case you want to make computation with vectors, matrixes, tensors, etc .. NumPy is probably a good option. I didn't understand exactly what you've asked, though. Can you explain me what you're asking?

• Yes, this is what I required, but it neither analysis nor computation on vectors, matrices, or tensors. It about the computation specific to distance calculation an efficient manner, and the algorithm I wrote, this formation was an integral part. Thanks. Dec 24, 2018 at 4:40