0
import numpy as np
a=np.array([ [1,2,3],[4,5,6],[7,8,9]])
  • How can I get zeroth index column? Expecting output [[1],[2],[3]] a[...,0] gives 1D array. Maybe next question answers this question.

  • How to get last 2 columns of a? a[...,1:2] gives second column only, a[...,2:3] gives last 2 columns, but a[...,3] is invalid dimension. So, how does it work?

By the way, operator ... and : have same meaning? a[...,0] and a[:,0] give same output. Can someone comment here?

2
  • 1
    It's no zero-th index column, but transposed first row. Try: a[:,0].T to get there. NB print(a) to see what's column, what's row Commented Jan 29, 2020 at 16:47
  • 1
    @GrzegorzSkibinski, a[:,0] is 1d, so .T does nothing. Commented Jan 29, 2020 at 17:24

2 Answers 2

1

numpy indexing is built on python list conventions, but extended to multi-dimensions and multi-element indexing. It is powerful, but complex, but sooner or later you should read a full indexing documentation, one that distinguishes between 'basic' and 'advanced' indexing.

Like range and arange, slice index has a 'open' stop value

In [111]: a = np.arange(1,10).reshape(3,3)                                                       
In [112]: a                                                                                      
Out[112]: 
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

Indexing with a scalar reduces the dimension, regardless of where:

In [113]: a[1,:]                                                                                 
Out[113]: array([4, 5, 6])
In [114]: a[:,1]                                                                                 
Out[114]: array([2, 5, 8])

That also means a[1,1] returns 5, not np.array([[5]]).

Indexing with a slice preserves the dimension:

In [115]: a[1:2,:]                                                                               
Out[115]: array([[4, 5, 6]])

so does indexing with a list or array (though this makes a copy, not a view):

In [116]: a[[1],:]                                                                               
Out[116]: array([[4, 5, 6]])

... is a generalized : - use as many as needed.

In [117]: a[...,[1]]                                                                             
Out[117]: 
array([[2],
       [5],
       [8]])

You can adjust dimensions with newaxis or reshape:

In [118]: a[:,1,np.newaxis]                                                                      
Out[118]: 
array([[2],
       [5],
       [8]])

Note that trailing : are automatic. a[1] is the same as a[1,:]. But leading ones must be explicit.

List indexing also removes a 'dimension/nesting layer'

In [119]: alist = [[1,2,3],[4,5,6]]                                                              
In [120]: alist[0]                                                                               
Out[120]: [1, 2, 3]
In [121]: alist[0][0]                                                                            
Out[121]: 1
In [122]: [l[0] for l in alist]     # a column equivalent                                                                  
Out[122]: [1, 4]
Sign up to request clarification or add additional context in comments.

Comments

0
import numpy as np
a=np.array([ [1,2,3],[4,5,6],[7,8,9]])

a[:,0] # first colomn
>>> array([1, 4, 7]) 
a[0,:] # first row
>>> array([1, 2, 3])
a[:,0:2] # first two columns
>>> array([[1, 2],
       [4, 5],
       [7, 8]])
a[0:2,:] # first two rows
>>> array([[1, 2, 3],
       [4, 5, 6]])

1 Comment

a[:,0] # first colomn doesn't column start with zero-index (as in cpython objects)?

Your Answer

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.