indexing a numpy array to the last column [duplicate]
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
python numpy indexing slice
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.
add a comment |
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
python numpy indexing slice
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
add a comment |
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
python numpy indexing slice
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
python numpy indexing slice
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 12 '18 at 17:26
Dinari
1,621422
1,621422
add a comment |
add a comment |
1
a[:, -2:]
; use an open ended slice.– hpaulj
Nov 12 '18 at 17:19