The np.nan is a constant representing a missing or undefined numerical value in a NumPy array. It stands for not a number and has a float type.
Syntax
numpy.nan
Example 1: Usage of np.nan() Method
import numpy as np
print(np.nan)
Output
nan
Example 2: Numpy array with nan value
import numpy as np
myarr = np.array([1, 0, np.nan, 3])
print(myarr)
Output
[ 1. 0. nan 3.]
The important thing I would like you to take away from this is that all of our integers have been converted to floats because NumPy has defined the NaN data type as a float.
Example 3: Comparing np.nan values
You can use the “double equal(==)” operator to compare two nan values in Python.
import numpy as np
print(np.nan == np.nan)
Output
False
You can see that two np.nan values are not the same.
Example 4: Checking for NaN values
To check for NaN values in an array, use the np.isnan() method.
Syntax
np.isnan(arr)
Visual representation
Here is an example.
import numpy as np
arr = np.nan
arr2 = 1
print(np.isnan(np.nan))
print(np.isnan(arr2))
Output
True
False
Example 5: NaN in Pandas Dataframe
In pandas, “NaN” stands for “Not a Number” and is the standard missing data marker. It’s essentially Python marking data as “not present” or “unknown”.
import numpy as np
import pandas as pd
df = pd.DataFrame([(1.0, np.nan, -1.0, 21.0),
(np.nan, 12.0, np.nan, 11),
(21.0, 15.0, np.nan, 91.0),
(np.nan, 14.0, -31.0, 19.0)],
columns=list('abcd'))
print(df)
Output
Example 6: Checking for NaN values in Pandas DataFrame
You can check for NaN values by using the isnull() method. The output will be a boolean mask with dimensions of the original dataframe.
import numpy as np
import pandas as pd
df = pd.DataFrame([(1.0, np.nan, -1.0, 21.0),
(np.nan, 12.0, np.nan, 11),
(21.0, 15.0, np.nan, 91.0),
(np.nan, 14.0, -31.0, 19.0)],
columns=list('abcd'))
print(pd.isnull(df))
Output
Example 7: Replacing NaN values in DataFrame
To replace NaN values in a Pandas Dataframe, you can “use the df.fillna() method.”
import numpy as np
import pandas as pd
df = pd.DataFrame([(1.0, np.nan, -1.0, 21.0),
(np.nan, 12.0, np.nan, 11),
(21.0, 15.0, np.nan, 91.0),
(np.nan, 14.0, -31.0, 19.0)],
columns=list('abcd'))
print(df.fillna(0))
Output
Example 8: Dropping rows containing NaN values
To drop the rows or columns with NaNs, you can use the .dropna() method.
import numpy as np
import pandas as pd
df = pd.DataFrame([(1.0, np.nan, -1.0, 21.0),
(np.nan, 12.0, np.nan, 11),
(21.0, 15.0, np.nan, 91.0),
(np.nan, 14.0, -31.0, 19.0)],
columns=list('abcd'))
print(df.dropna(axis="columns"))
Output
d
0 21.0
1 11.0
2 91.0
3 19.0
That’s it.