0

I have the following numpy array:

a=[['Sb' array([4.24035696, 2.44817292, 7.41858935])]
 ['I' array([2.08076032, 3.69501392, 5.37518666])]
 ['I' array([6.35173963, 1.22916823, 8.9947238 ])]
 ['I' array([ 4.24036048, -0.04551256,  5.37518684])]
 ['I' array([4.24035383, 4.88618543, 8.99472472])]
 ['I' array([6.3999514 , 3.69501663, 5.37518685])]
 ['I' array([2.12897688, 1.22916548, 8.99472467])]]

I'd like to unpack the inner array to have:

a=[['Sb' 4.24035696 2.44817292 7.41858935]
 ...
 ['I' 2.12897688 1.22916548 8.99472467]]

I can't figure this one out in a simple way.

1
  • 1
    What have you tried? It isn't a simple structure; so don't expect a simple fix. It's a (7,2) object dtype array. a[:,0] is the characters column. a[:,1] the arrays. np.stack(a[:,1]) will give you a (7,3) float array. That could be concatenated with the character one, but getting the right dtype could be tricky. String and floats don't coexist in one array easily. Commented Jun 18, 2022 at 16:08

1 Answer 1

1

See my comment for explanation:

In [142]: array=np.array
     ...: a=np.array([['Sb', array([4.24035696, 2.44817292, 7.41858935])],
     ...:  ['I', array([2.08076032, 3.69501392, 5.37518666])],
     ...:  ['I', array([6.35173963, 1.22916823, 8.9947238 ])],
     ...:  ['I', array([ 4.24036048, -0.04551256,  5.37518684])],
     ...:  ['I', array([4.24035383, 4.88618543, 8.99472472])],
     ...:  ['I', array([6.3999514 , 3.69501663, 5.37518685])],
     ...:  ['I', array([2.12897688, 1.22916548, 8.99472467])]], dtype=object)

In [143]: a
Out[143]: 
array([['Sb', array([4.24035696, 2.44817292, 7.41858935])],
       ['I', array([2.08076032, 3.69501392, 5.37518666])],
       ['I', array([6.35173963, 1.22916823, 8.9947238 ])],
       ['I', array([ 4.24036048, -0.04551256,  5.37518684])],
       ['I', array([4.24035383, 4.88618543, 8.99472472])],
       ['I', array([6.3999514 , 3.69501663, 5.37518685])],
       ['I', array([2.12897688, 1.22916548, 8.99472467])]], dtype=object)

In [144]: a[:,0]
Out[144]: array(['Sb', 'I', 'I', 'I', 'I', 'I', 'I'], dtype=object)

In [145]: np.stack(a[:,1])
Out[145]: 
array([[ 4.24035696,  2.44817292,  7.41858935],
       [ 2.08076032,  3.69501392,  5.37518666],
       [ 6.35173963,  1.22916823,  8.9947238 ],
       [ 4.24036048, -0.04551256,  5.37518684],
       [ 4.24035383,  4.88618543,  8.99472472],
       [ 6.3999514 ,  3.69501663,  5.37518685],
       [ 2.12897688,  1.22916548,  8.99472467]])

Combine them into an object dtype array:

In [148]: res = np.concatenate((a[:,[0]],np.stack(a[:,1])),axis=1,dtype=object)

In [149]: res
Out[149]: 
array([['Sb', 4.24035696, 2.44817292, 7.41858935],
       ['I', 2.08076032, 3.69501392, 5.37518666],
       ['I', 6.35173963, 1.22916823, 8.9947238],
       ['I', 4.24036048, -0.04551256, 5.37518684],
       ['I', 4.24035383, 4.88618543, 8.99472472],
       ['I', 6.3999514, 3.69501663, 5.37518685],
       ['I', 2.12897688, 1.22916548, 8.99472467]], dtype=object)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes not straightforward, I tried a few functions to combine the separate arrays but the np.stack(a[:,1]) was the important trick missing. Thank you very much.

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.