a = pd.DataFrame({
"one":[np.NAN,np.NAN,"e"],
"two":["a","s","d"],
"three":[1,2,2]
})
one | two | three | |
---|---|---|---|
0 | nan | a | 1 |
1 | nan | s | 2 |
2 | e | d | 2 |
I want to change values in column "one" that meet conditions below
- a["one"] is null.
- a["three] is 1.
so i used boolean indexing.
a[(a["one"].isnull()) & (a["three"]==1)]
one | two | three | |
---|---|---|---|
0 | nan | a | 1 |
a[(a["one"].isnull()) & (a["three"]==1)].loc[a["one"]!=a["one"], "one"]
one | |
---|---|
0 | nan |
a[(a["one"].isnull()) & (a["three"]==1)].loc[a["one"]!=a["one"], "one"] = "replaced"
But dataframe "a" is still "a". Nan value isn't replaced as "replaced". nothing changed.
I want to know why.
CodePudding user response:
But dataframe "a" is still "a". Nan value isn't replaced as "replaced". nothing changed. I want to know why.
If check evaluation order matters
there is explained you assign to view a[(a["one"].isnull()) & (a["three"]==1)]
not to original DataFrame.
Correct way is use DataFrame.loc
for set by boolean mask and columns name:
a.loc[a["one"].isna() & (a["three"]==1), "one"] = "replaced"
print (a)
one two three
0 replaced a 1
1 NaN s 2
2 e d 2