Pandas DataFrame.fillna()
We can use the fillna() function to fill the null values in the dataset.
Syntax:
DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None, **kwargs)
Parameters:
- value: It is a value that is used to fill the null values, alternately a Series/dict/DataFrame.
- method: A method that is used to fill the null values in the reindexed Series.
- axis: It takes int or string value for rows/columns. Axis along which we need to fill missing values.
- inplace: If it is True, it fills values at an empty place.
- limit: It is an integer value that specifies the maximum number of consecutive forward/backward NaN value fills.
- downcast: It takes a dict that specifies what to downcast like Float64 to int64.
Returns:
It returns an object in which the missing values are being filled.
Example1:
import pandas as pd # Create a dataframe info = pd.DataFrame(data={'x':[10,20,30,40,50,None]}) print(info) # Fill null value to dataframe using 'inplace' info.fillna(value=0, inplace=True) print(info)
Output
x
0 10.0
1 20.0
2 30.0
3 40.0
4 50.0
5 NaN
x
0 10.0
1 20.0
2 30.0
3 40.0
4 50.0
5 0.0
Example2:
The below code is responsible for filling the DataFrame that consist some NaN values.
import pandas as pd # Create a dataframe info = pd.DataFrame([[np.nan,np.nan, 20, 0], [1, np.nan, 4, 1], [np.nan, np.nan, np.nan, 5], [np.nan, 20, np.nan, 2]], columns=list('ABCD')) info
Output
A B C D
0 NaN NaN 20.0 0
1 1.0 NaN 4.0 1
2 NaN NaN NaN 5
3 NaN 20.0 NaN 2
Example3:
In below code, we have used the fillna function to fill in some of the NaN values only.
info = pd.DataFrame([[np.nan,np.nan, 20, 0], [1, np.nan, 4, 1], [np.nan, np.nan, np.nan, 5], [np.nan, 20, np.nan, 2]], columns=list('ABCD')) info info.fillna(0) info.fillna(method='ffill') values = {'A': 0, 'B': 1, 'C': 2, 'D': 3} info.fillna(value=values) info.fillna(value=values, limit=1)
Output
A B C D
0 0.0 1.0 20.0 0
1 1.0 NaN 4.0 1
2 NaN NaN 2.0 5
3 NaN 20.0 NaN 2