1

I have an np.ndarray:

[[0 4 0 0] [0 5 8 2] [2 7 3 2]]

I want to append in position 0 another element so that the output looks like:

[['cat' 0 4 0 0] ['cat' 0 5 8 2] ['cat' 2 7 3 2]]

I've tried np.insert, append, vstack, and concatenate as indicated in other posts. But, none work. I suspect this is because the type of my array is <class 'numpy.ndarray'>. I've also tried converting it to a list and appending it or to a normal np.array, but nothing seems to work.

Any ideas?

Edit:

I tried:

    arr=[np.append('cat',i) for i in my_array]

This works, however, the output looks like:

[array(['cat' 0 4 0 0], dtype=object), array(['cat' 0 5 8 2], dtype=object), array(['cat' 2 7 3 2], dtype=object)]
9
  • 1
    numpy arrays like being one datatype. Commented Dec 30, 2018 at 20:22
  • @ParitoshSingh but even adding an int doesn't work... Commented Dec 30, 2018 at 20:24
  • and why should it? your example is trying to add "cat". thats not an int, and cant be typecast to one. Commented Dec 30, 2018 at 20:29
  • Why didn't it work with listsl? Have you tried [['cat'] + i for i in l] Commented Dec 30, 2018 at 20:30
  • @ParitoshSingh I understand that cat is a string.. I was just telling you that I had tried inserting an int too Commented Dec 30, 2018 at 20:31

1 Answer 1

2

An ndarray is a container for homogenous data; that is, every element must be of the same type.

To accomplish what you're trying to do with the correct type, it's as simple as using insert. In this example, I insert -1 at index 0 along the column axis:

>>> a
array([[0, 4, 0, 0],
       [0, 5, 8, 2],
       [2, 7, 3, 2]])
>>> np.insert(a, 0, -1, axis=1)
array([[-1,  0,  4,  0,  0],
       [-1,  0,  5,  8,  2],
       [-1,  2,  7,  3,  2]])
Sign up to request clarification or add additional context in comments.

6 Comments

@user3755632 it may give the correct output but "works" is loose here; it a) completely trashes any hope of getting any benefit of numpy beyond advanced indexing (no vectorized operations will work), and b) is extremely inefficient if done more than a couple of times because shape changes force a full copy of the entire array - lists are generally faster.
@roganjosh Should I delete this answer? I'm not sure I fully understand your "a)" point.
Absolutely not what I'm suggesting, sorry. It's more a note to the OP that, while this gives them their desired output, it is not really in-line with what numpy is for. They might want to review the overall approach.
Numpy can drop calculations straight down into C and use vectorization; this can make certain calculations orders of magnitude faster. While your example is totally numerical, I got the impression (perhaps incorrectly) that the OP wants arrays of mixed type, and vectorization will then become impossible
:) My comment should have been worded much more clearly, I didn't mean to imply the answer was technically incorrect, apologies.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.