Reputation: 8164
My code:
#!/usr/bin/python
import numpy as np
names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
data = np.random.randn(7, 4) + 0.8
print (data)
mask2= ((names != 'Joe') == 7.0)
d2 = data[mask2]
print (d2)
d3 = data[names != 'Joe'] = 7.0
print (d3)
Actually,my intention was to get the same solution both with mask and with other expression. I have solved it with Patric,s help
mask2= (names != 'Joe')
data[mask2] = 7.0
print (data)
Then I have:
[[ 7. 7. 7. 7. ]
[-0.73168514 2.26996071 -0.24892468 1.31421193]
[ 7. 7. 7. 7. ]
[ 7. 7. 7. 7. ]
[ 7. 7. 7. 7. ]
[ 0.74771766 2.44888399 0.62641731 -0.12963696]
[ 0.08604169 2.25468039 2.1960925 0.88218726]]
Upvotes: 0
Views: 96
Reputation: 1603
Not sure to understand, but if you expect 7.0 in all rows except Joe's, maybe what you want is :
data[names != 'Joe'] = 7.0
print data
Upvotes: 1
Reputation: 55448
mask2 = ((names != 'Joe') == 7.0)
Why my mask failed in Python?
This mask doesn't make sense, with that expression, you are compared the result of names != 'Joe'
with 7.0
In [13]: names != 'Joe'
Out[13]: array([ True, False, True, True, True, False, False], dtype=bool)
So it's natural that this will get you all False
everywhere:
In [14]: ((names != 'Joe') == 7.0)
Out[14]: array([False, False, False, False, False, False, False], dtype=bool)
Your other mask makes sense, something in this form:
x[mask] = value
Upvotes: 2