How to change value in pandas dataframe?

Question:

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

  1. a["one"] is null.
  2. 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.

Asked By: 신동혁

||

Answers:

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
Answered By: jezrael
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.