8

I tried to solve the required task with the following code line:

df['Age'][np.isnan(df["Age"])] = rand1

enter image description here

But this raises a "SettingWithCopyWarning" and I think locating the Nan values in the dataframe (Column 'Age') by using the .loc feature might be a better way of doing this.

I already took a look at the documentation, but still don't know how I can fix this problem. Couldn't find any solutions on here with .loc either.

I would appreciate any hints and advice.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
ErnieandBert
  • 91
  • 1
  • 1
  • 8

1 Answers1

9

You need fillna for replace NaN to some value:

df.Age = df.Age.fillna(rand1)

Your solution with loc:

df.loc[np.isnan(df["Age"]), 'Age'] = rand1
#same as
#df.loc[df["Age"].isnull(), 'Age'] = rand1

You can also check indexing view versus copy.

Sample:

df = pd.DataFrame({'Age':[20,23,np.nan]})
print (df)
    Age
0  20.0
1  23.0
2   NaN

rand1 = 30
df.Age = df.Age.fillna(rand1)
print (df)
    Age
0  20.0
1  23.0
2  30.0

#if need cast to int
df.Age = df.Age.fillna(rand1).astype(int)
print (df)
   Age
0   20
1   23
2   30
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thanks a lot for your help. Code works perfectly fine. Can you please explain what is happening inside the .loc function? I initially thought it's for accessing indexes such as Age. Why are we passing in the .isnan part and then once again the Age column? Really have a hard time understanding this, even after reading the documentation. – ErnieandBert Feb 24 '17 at 22:37
  • Ok, it works because `np.isnan(df['Age'])` return boolean mask and with combination with `loc` set values to `rand1` where `True` values. I think better explanation is in this [pandas tutorial](http://tomaugspurger.github.io/modern-1.html) - check title `SettingWithCopy` (there use another mask `f['a'] <= 3` instead `np.isnan(df['Age'])`) – jezrael Feb 24 '17 at 22:43
  • And this tutorial is [here](http://pandas.pydata.org/pandas-docs/stable/tutorials.html) - `modern pandas` (very nice explanations) – jezrael Feb 24 '17 at 22:45