Reputation: 42349
I'm attempting to perform a simple task: append an array to the beginning of another array. Here a MWE of what I mean:
a = ['a','b','c','d','e','f','g','h','i']
b = [6,4,1.,2,8,784.,43,6.,2]
c = [8,4.,32.,6,1,7,2.,9,23]
# Define arrays.
a_arr = np.array(a)
bc_arr = np.array([b, c])
# Append a_arr to beginning of bc_arr
print np.concatenate((a_arr, bc_arr), axis=1)
but I keep getting a ValueError: all the input arrays must have same number of dimensions
error.
The arrays a_arr
and bc_arr
come like that from a different process so I can't manipulate the way they are created (ie: I can't use the a,b,c
lists).
How can I generate a new array of a_arr
and bc_arr
so that it will look like:
array(['a','b','c','d','e','f','g','h','i'], [6,4,1.,2,8,784.,43,6.,2], [8,4.,32.,6,1,7,2.,9,23])
Upvotes: 0
Views: 295
Reputation: 180441
Can you do something like.
In [88]: a = ['a','b','c','d','e','f','g','h','i']
In [89]: b = [6,4,1.,2,8,784.,43,6.,2]
In [90]: c = [8,4.,32.,6,1,7,2.,9,23]
In [91]: joined_arr=np.array([a_arr,b_arr,c_arr],dtype=object)
In [92]: joined_arr
Out[92]:
array([['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'],
[6.0, 4.0, 1.0, 2.0, 8.0, 784.0, 43.0, 6.0, 2.0],
[8.0, 4.0, 32.0, 6.0, 1.0, 7.0, 2.0, 9.0, 23.0]], dtype=object)
Upvotes: 1
Reputation: 6361
this should work
In [84]: a=np.atleast_2d(a).astype('object')
In [85]: b=np.atleast_2d(b).astype('object')
In [86]: c=np.atleast_2d(c).astype('object')
In [87]: np.vstack((a,b,c))
Out[87]:
array([[a, b, c, d, e, f, g, h, i],
[6.0, 4.0, 1.0, 2.0, 8.0, 784.0, 43.0, 6.0, 2.0],
[8.0, 4.0, 32.0, 6.0, 1.0, 7.0, 2.0, 9.0, 23.0]], dtype=object)
Upvotes: 1