How to use fillna() In Python Pandas
We can use the fillna()
method to replace NaN values in a DataFrame.
df = df.fillna()
Python3
import pandas as pd import numpy as np car = pd.DataFrame({ 'Year of Launch' : [ 1999 , np.nan, 1986 , 2020 , np.nan, 1991 ], 'Engine Number' : [np.nan, 15 , 22 , 43 , 44 , np.nan], 'Chasis Unique Id' : [ 4023 , np.nan, 3115 , 4522 , 3643 , 3774 ]}) car |
Output:
Year of Launch Engine Number Chasis Unique Id
0 1999.0 NaN 4023.0
1 NaN 15.0 NaN
2 1986.0 22.0 3115.0
3 2020.0 43.0 4522.0
4 NaN 44.0 3643.0
5 1991.0 NaN 3774.0
Python3
car_filled = car.fillna( 0 ) car_filled |
Output:
Year of Launch Engine Number Chasis Unique Id
0 1999.0 0.0 4023.0
1 0.0 15.0 0.0
2 1986.0 22.0 3115.0
3 2020.0 43.0 4522.0
4 0.0 44.0 3643.0
5 1991.0 0.0 3774.0
All nan values has been replaced by 0.
How to Drop Rows with NaN Values in Pandas DataFrame?
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. In this article, we will discuss how to drop rows with NaN values.