Python append to ndarray

Multi tool use
Multi tool use












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)]









share|improve this question




















  • 1





    numpy arrays like being one datatype.

    – Paritosh Singh
    Dec 30 '18 at 20:22











  • @ParitoshSingh but even adding an int doesn't work...

    – user3755632
    Dec 30 '18 at 20:24











  • and why should it? your example is trying to add "cat". thats not an int, and cant be typecast to one.

    – Paritosh Singh
    Dec 30 '18 at 20:29













  • Why didn't it work with listsl? Have you tried [['cat'] + i for i in l]

    – yatu
    Dec 30 '18 at 20:30













  • @ParitoshSingh I understand that cat is a string.. I was just telling you that I had tried inserting an int too

    – user3755632
    Dec 30 '18 at 20:31
















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)]









share|improve this question




















  • 1





    numpy arrays like being one datatype.

    – Paritosh Singh
    Dec 30 '18 at 20:22











  • @ParitoshSingh but even adding an int doesn't work...

    – user3755632
    Dec 30 '18 at 20:24











  • and why should it? your example is trying to add "cat". thats not an int, and cant be typecast to one.

    – Paritosh Singh
    Dec 30 '18 at 20:29













  • Why didn't it work with listsl? Have you tried [['cat'] + i for i in l]

    – yatu
    Dec 30 '18 at 20:30













  • @ParitoshSingh I understand that cat is a string.. I was just telling you that I had tried inserting an int too

    – user3755632
    Dec 30 '18 at 20:31














1












1








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)]









share|improve this question
















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)]






python arrays numpy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 30 '18 at 20:40







user3755632

















asked Dec 30 '18 at 20:15









user3755632user3755632

168112




168112








  • 1





    numpy arrays like being one datatype.

    – Paritosh Singh
    Dec 30 '18 at 20:22











  • @ParitoshSingh but even adding an int doesn't work...

    – user3755632
    Dec 30 '18 at 20:24











  • and why should it? your example is trying to add "cat". thats not an int, and cant be typecast to one.

    – Paritosh Singh
    Dec 30 '18 at 20:29













  • Why didn't it work with listsl? Have you tried [['cat'] + i for i in l]

    – yatu
    Dec 30 '18 at 20:30













  • @ParitoshSingh I understand that cat is a string.. I was just telling you that I had tried inserting an int too

    – user3755632
    Dec 30 '18 at 20:31














  • 1





    numpy arrays like being one datatype.

    – Paritosh Singh
    Dec 30 '18 at 20:22











  • @ParitoshSingh but even adding an int doesn't work...

    – user3755632
    Dec 30 '18 at 20:24











  • and why should it? your example is trying to add "cat". thats not an int, and cant be typecast to one.

    – Paritosh Singh
    Dec 30 '18 at 20:29













  • Why didn't it work with listsl? Have you tried [['cat'] + i for i in l]

    – yatu
    Dec 30 '18 at 20:30













  • @ParitoshSingh I understand that cat is a string.. I was just telling you that I had tried inserting an int too

    – user3755632
    Dec 30 '18 at 20:31








1




1





numpy arrays like being one datatype.

– Paritosh Singh
Dec 30 '18 at 20:22





numpy arrays like being one datatype.

– Paritosh Singh
Dec 30 '18 at 20:22













@ParitoshSingh but even adding an int doesn't work...

– user3755632
Dec 30 '18 at 20:24





@ParitoshSingh but even adding an int doesn't work...

– user3755632
Dec 30 '18 at 20:24













and why should it? your example is trying to add "cat". thats not an int, and cant be typecast to one.

– Paritosh Singh
Dec 30 '18 at 20:29







and why should it? your example is trying to add "cat". thats not an int, and cant be typecast to one.

– Paritosh Singh
Dec 30 '18 at 20:29















Why didn't it work with listsl? Have you tried [['cat'] + i for i in l]

– yatu
Dec 30 '18 at 20:30







Why didn't it work with listsl? Have you tried [['cat'] + i for i in l]

– yatu
Dec 30 '18 at 20:30















@ParitoshSingh I understand that cat is a string.. I was just telling you that I had tried inserting an int too

– user3755632
Dec 30 '18 at 20:31





@ParitoshSingh I understand that cat is a string.. I was just telling you that I had tried inserting an int too

– user3755632
Dec 30 '18 at 20:31












1 Answer
1






active

oldest

votes


















1














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]])





share|improve this answer


























  • Thanks! It worked by adding the string too :)

    – user3755632
    Dec 30 '18 at 20:42











  • @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
    Dec 30 '18 at 21:11













  • @roganjosh Should I delete this answer? I'm not sure I fully understand your "a)" point.

    – cody
    Dec 30 '18 at 21:20











  • 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.

    – roganjosh
    Dec 30 '18 at 21:24













  • 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

    – roganjosh
    Dec 30 '18 at 21:26











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53981091%2fpython-append-to-ndarray%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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]])





share|improve this answer


























  • Thanks! It worked by adding the string too :)

    – user3755632
    Dec 30 '18 at 20:42











  • @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
    Dec 30 '18 at 21:11













  • @roganjosh Should I delete this answer? I'm not sure I fully understand your "a)" point.

    – cody
    Dec 30 '18 at 21:20











  • 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.

    – roganjosh
    Dec 30 '18 at 21:24













  • 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

    – roganjosh
    Dec 30 '18 at 21:26
















1














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]])





share|improve this answer


























  • Thanks! It worked by adding the string too :)

    – user3755632
    Dec 30 '18 at 20:42











  • @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
    Dec 30 '18 at 21:11













  • @roganjosh Should I delete this answer? I'm not sure I fully understand your "a)" point.

    – cody
    Dec 30 '18 at 21:20











  • 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.

    – roganjosh
    Dec 30 '18 at 21:24













  • 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

    – roganjosh
    Dec 30 '18 at 21:26














1












1








1







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]])





share|improve this answer















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]])






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 30 '18 at 20:41

























answered Dec 30 '18 at 20:27









codycody

3,98621124




3,98621124













  • Thanks! It worked by adding the string too :)

    – user3755632
    Dec 30 '18 at 20:42











  • @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
    Dec 30 '18 at 21:11













  • @roganjosh Should I delete this answer? I'm not sure I fully understand your "a)" point.

    – cody
    Dec 30 '18 at 21:20











  • 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.

    – roganjosh
    Dec 30 '18 at 21:24













  • 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

    – roganjosh
    Dec 30 '18 at 21:26



















  • Thanks! It worked by adding the string too :)

    – user3755632
    Dec 30 '18 at 20:42











  • @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
    Dec 30 '18 at 21:11













  • @roganjosh Should I delete this answer? I'm not sure I fully understand your "a)" point.

    – cody
    Dec 30 '18 at 21:20











  • 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.

    – roganjosh
    Dec 30 '18 at 21:24













  • 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

    – roganjosh
    Dec 30 '18 at 21:26

















Thanks! It worked by adding the string too :)

– user3755632
Dec 30 '18 at 20:42





Thanks! It worked by adding the string too :)

– user3755632
Dec 30 '18 at 20:42













@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
Dec 30 '18 at 21:11







@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
Dec 30 '18 at 21:11















@roganjosh Should I delete this answer? I'm not sure I fully understand your "a)" point.

– cody
Dec 30 '18 at 21:20





@roganjosh Should I delete this answer? I'm not sure I fully understand your "a)" point.

– cody
Dec 30 '18 at 21:20













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.

– roganjosh
Dec 30 '18 at 21:24







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.

– roganjosh
Dec 30 '18 at 21:24















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

– roganjosh
Dec 30 '18 at 21:26





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

– roganjosh
Dec 30 '18 at 21:26


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53981091%2fpython-append-to-ndarray%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Ne,gC,UgvOCCnk5Pd9kgEh5 sFSkbK qsuRJ7KR gWFo13lB12sdi6tF5 T6OpaOCRV6 LksCEQb PemFcN5,v lKpC,7Sr8kPYjm07AOAdtg
7j3QPbQd7MZscYtjV F,LYW1vI4 WrJKx,T8Lip2LuTlBRmDwsnkg21bVHeclwcUloBAxha

Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas