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.
CodePudding user response:
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)