I tried to solve the required task with the following code line:
df['Age'][np.isnan(df["Age"])] = rand1
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.
Use df. replace(np. nan,'',regex=True) method to replace all NaN values to an empty string in the Pandas DataFrame column.
You can use the fillna() function to replace NaN values in a pandas DataFrame.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With