Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change value in pandas dataframe?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading