Reputation: 806
df1 = pd.DataFrame(np.random.random_sample((10)), columns=list('A'))
df1['MATCHED'] = 0
df1
A MATCHED
0 0.424651 0
1 0.855567 0
2 0.983395 0
3 0.921866 0
4 0.001827 0
5 0.341491 0
6 0.055578 0
7 0.970564 0
8 0.078751 0
9 0.348055 0
Then I filter df1
:
df1_slice = df1[df1['A'] <=0.4]
df1_slice
A MATCHED
4 0.001827 0
5 0.341491 0
6 0.055578 0
8 0.078751 0
9 0.348055 0
Now, I want to change the column MATCHED values for those rows in df1_slice
:
df1.loc[df1_slice.index]['MATCHED']=1
I'd expect the MATCHED column changes in df1
from 0 to 1, but it doesn't.
df1.loc[df1_slice.index]
A MATCHED
4 0.001827 0
5 0.341491 0
6 0.055578 0
8 0.078751 0
9 0.348055 0
Why they don't change and how to change this script so that they change to 1 in df1
.
Upvotes: 0
Views: 30
Reputation: 921
I find that using .loc throughout is the best way to (re-)assign to slices. In your example you use .loc only to slice on the index, not on the columns. This should do it (haven't tested):
df1.loc[df1_slice.index, 'MATCHED'] = 1
Upvotes: 1