indexing a numpy array to the last column [duplicate]












0















This question already has an answer here:




  • Understanding Python's slice notation

    31 answers




I'd like to replace the last N columns of a Numpy array by zeroes.



a = numpy.tile([1,2,3,4], (4,1))

array([[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]])


To replace the last, I do:



a[:,-1] = 0


To replace the last 2, I would do



a[:,-2:-1] = 0


but this only replaces the penultimate column



array([[1, 2, 0, 4],
[1, 2, 0, 4],
[1, 2, 0, 4],
[1, 2, 0, 4]])


Can you please explain why?
What should I do to get what I want?



Thanks










share|improve this question















marked as duplicate by jpp, Community Nov 12 '18 at 22:11


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    a[:, -2:]; use an open ended slice.
    – hpaulj
    Nov 12 '18 at 17:19
















0















This question already has an answer here:




  • Understanding Python's slice notation

    31 answers




I'd like to replace the last N columns of a Numpy array by zeroes.



a = numpy.tile([1,2,3,4], (4,1))

array([[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]])


To replace the last, I do:



a[:,-1] = 0


To replace the last 2, I would do



a[:,-2:-1] = 0


but this only replaces the penultimate column



array([[1, 2, 0, 4],
[1, 2, 0, 4],
[1, 2, 0, 4],
[1, 2, 0, 4]])


Can you please explain why?
What should I do to get what I want?



Thanks










share|improve this question















marked as duplicate by jpp, Community Nov 12 '18 at 22:11


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    a[:, -2:]; use an open ended slice.
    – hpaulj
    Nov 12 '18 at 17:19














0












0








0








This question already has an answer here:




  • Understanding Python's slice notation

    31 answers




I'd like to replace the last N columns of a Numpy array by zeroes.



a = numpy.tile([1,2,3,4], (4,1))

array([[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]])


To replace the last, I do:



a[:,-1] = 0


To replace the last 2, I would do



a[:,-2:-1] = 0


but this only replaces the penultimate column



array([[1, 2, 0, 4],
[1, 2, 0, 4],
[1, 2, 0, 4],
[1, 2, 0, 4]])


Can you please explain why?
What should I do to get what I want?



Thanks










share|improve this question
















This question already has an answer here:




  • Understanding Python's slice notation

    31 answers




I'd like to replace the last N columns of a Numpy array by zeroes.



a = numpy.tile([1,2,3,4], (4,1))

array([[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]])


To replace the last, I do:



a[:,-1] = 0


To replace the last 2, I would do



a[:,-2:-1] = 0


but this only replaces the penultimate column



array([[1, 2, 0, 4],
[1, 2, 0, 4],
[1, 2, 0, 4],
[1, 2, 0, 4]])


Can you please explain why?
What should I do to get what I want?



Thanks





This question already has an answer here:




  • Understanding Python's slice notation

    31 answers








python numpy indexing slice






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 18:07









jpp

92.6k2054103




92.6k2054103










asked Nov 12 '18 at 17:16









Eric Chassande-Mottin

83




83




marked as duplicate by jpp, Community Nov 12 '18 at 22:11


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by jpp, Community Nov 12 '18 at 22:11


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    a[:, -2:]; use an open ended slice.
    – hpaulj
    Nov 12 '18 at 17:19














  • 1




    a[:, -2:]; use an open ended slice.
    – hpaulj
    Nov 12 '18 at 17:19








1




1




a[:, -2:]; use an open ended slice.
– hpaulj
Nov 12 '18 at 17:19




a[:, -2:]; use an open ended slice.
– hpaulj
Nov 12 '18 at 17:19












1 Answer
1






active

oldest

votes


















2














When doing a[:,-2:-1] = 0 you are picking from the 1 before the last column, until the last column, not including the last column.



What you are looking for is a[:,-2:] = 0 , which will pick all the columns from -2 to the end. Similarly, a[:,-3:-1] will pick the 2 middle columns.






share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    When doing a[:,-2:-1] = 0 you are picking from the 1 before the last column, until the last column, not including the last column.



    What you are looking for is a[:,-2:] = 0 , which will pick all the columns from -2 to the end. Similarly, a[:,-3:-1] will pick the 2 middle columns.






    share|improve this answer


























      2














      When doing a[:,-2:-1] = 0 you are picking from the 1 before the last column, until the last column, not including the last column.



      What you are looking for is a[:,-2:] = 0 , which will pick all the columns from -2 to the end. Similarly, a[:,-3:-1] will pick the 2 middle columns.






      share|improve this answer
























        2












        2








        2






        When doing a[:,-2:-1] = 0 you are picking from the 1 before the last column, until the last column, not including the last column.



        What you are looking for is a[:,-2:] = 0 , which will pick all the columns from -2 to the end. Similarly, a[:,-3:-1] will pick the 2 middle columns.






        share|improve this answer












        When doing a[:,-2:-1] = 0 you are picking from the 1 before the last column, until the last column, not including the last column.



        What you are looking for is a[:,-2:] = 0 , which will pick all the columns from -2 to the end. Similarly, a[:,-3:-1] will pick the 2 middle columns.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 17:26









        Dinari

        1,621422




        1,621422















            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values