I have a numpy array of shape (3,2), where each cell is a numpy array of shape 2 (object):
df = pd.DataFrame({'A':[np.array([4,4]),np.array([5,5]),np.array([6,6])], 'B':[np.array([4,5]),np.array([5,6]),np.array([6,7])]})
df.head()
A B
0 [4, 4] [4, 5]
1 [5, 5] [5, 6]
2 [6, 6] [6, 7]
a = df.to_numpy()
print(a.shape) #gives (3,2)
print(a[0,0]) #gives array([4,4])
print(a[0,0].shape) #gives (2,)
print(a)
#Gives:
array([[array([4, 4]), array([4, 5])],
[array([5, 5]), array([5, 6])],
[array([6, 6]), array([6, 7])]], dtype=object)
How can I unpack the cells so that a
becomes of shape (3,2,2)?
CodePudding user response:
IIUC, you might want:
import numpy as np
np.c_[df.values.tolist()]
output:
array([[[4, 4],
[4, 5]],
[[5, 5],
[5, 6]],
[[6, 6],
[6, 7]]])