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])
asked May 17, 2014 at 13:40
Gabriel
43k83 gold badges248 silver badges434 bronze badges
2 Answers 2
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)
answered May 17, 2014 at 14:49
Padraic Cunningham
181k30 gold badges264 silver badges327 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Gabriel
This seems to work, I can use
bc_arr instead of b_arr,c_arr. Thanks.Padraic Cunningham
No worries. I missed the need to keep
bc_arr as it is.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)
answered May 17, 2014 at 14:21
Moj
6,3812 gold badges26 silver badges36 bronze badges
Comments
lang-py
numpyarray to store mixed datatypes, especiallychars? I'm not sure most of thenumpyfunctionality would be available to you if you use it. Why not use a simple list or a custom class?numpyarray...a_arr,bc_arr) should stay as a sub-array within the final array like show in the question.