[Tutor] How to edit value of second column row i-th array in numpy?

Peter Otten __peter__ at web.de
Thu Jan 15 19:33:35 CET 2015


Whees Northbee wrote:

> If I have array a=[[60,70,1],[43,54,2],[87,67,3],[90,89,4],[12,4,5]] where
> [x,y,id] and I want to find if x=87 and y=67, and find which row is it,and
> I need to edit the id column with new value..
> 
> I can search in first column using:
> if (x in a[:,0]):
>     print('yes')
> 
> how I can know index row so I can match if y same value with second
> column? 

I needed some google hand-holding for this, so no warranties:

>>> import numpy as np
>>> a = np.array([[60,70,1],[43,54,2],[87,67,3],[87,89,4],[12,4,5]])
>>> a[:,:2] == [87,67]
array([[False, False],
       [False, False],
       [ True,  True],
       [ True, False],
       [False, False]], dtype=bool)
>>> (a[:,:2] == [87,67]).all(axis=1)
array([False, False,  True, False, False], dtype=bool)
>>> np.where((a[:,:2] == [87,67]).all(axis=1))
(array([2]),)

> And I search in numpy array document there is no function about
> edit, so how I can edit the value the third column/id column if I know
> which row? 

Easy:

>>> a[2,2] = 123456
>>> a
array([[    60,     70,      1],
       [    43,     54,      2],
       [    87,     67, 123456],
       [    87,     89,      4],
       [    12,      4,      5]])




More information about the Tutor mailing list