0

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
6
  • Can I ask why you're using a numpy array to store mixed datatypes, especially chars? I'm not sure most of the numpy functionality would be available to you if you use it. Why not use a simple list or a custom class? Commented May 17, 2014 at 14:04
  • I'ts related to this question I made yesterday: stackoverflow.com/questions/23707082/… Commented May 17, 2014 at 14:09
  • I still don't understand what advantage you'd have using a numpy array... Commented May 17, 2014 at 14:31
  • do b and c have to be joined when you create arrays? Commented May 17, 2014 at 14:41
  • @PadraicCunningham no, each initial array (a_arr, bc_arr) should stay as a sub-array within the final array like show in the question. Commented May 17, 2014 at 14:42

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

This seems to work, I can use bc_arr instead of b_arr,c_arr. Thanks.
No worries. I missed the need to keep bc_arr as it is.
1

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.