I want to replace number 3 instead of all 'nan' in array. this is my code:
train= train.replace("nan",int(3))
But nothing changes in my array. Could u please guide me?
To replace all -9999 values with NaN, you can do: mintemp (mintemp == -9999) = NaN; If you have Statistics Toolbox, you can then use NANMEAN:
Steps to replace NaN values: 1 For one column using pandas: df ['DataFrame Column'] = df ['DataFrame Column'].fillna (0) 2 For one column using numpy: df ['DataFrame Column'] = df ['DataFrame Column'].replace (np.nan, 0) 3 For the whole DataFrame using pandas: df.fillna (0) 4 For the whole DataFrame using numpy: df.replace (np.nan, 0)
NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float. NaN value is one of the major problems in Data Analysis. It is very essential to deal with NaN in order to get the desired results.
Replace NaN Values with Zeros in Pandas DataFrame Last Updated : 03 Jul, 2020 NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float.
You can use np.isnan
:
import numpy as np
train = np.array([2, 4, 4, 8, 32, np.NaN, 12, np.NaN])
train[np.isnan(train)]=3
train
Output:
array([ 2., 4., 4., 8., 32., 3., 12., 3.])
>>> import math
>>> train = [10, float('NaN'), 20, float('NaN'), 30]
>>> train = [3 if math.isnan(x) else x for x in train]
>>> train
[10, 3, 20, 3, 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