Pandas.DataFrame.fillna() Function with examples to replace null values

Spread the love

Pandas.DataFrame.fillna() funtion : If you are working on data sceince and machine learning projects, if you get the data with null values, you can use this function to fill values with specific method.

Syntax :DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None, **kwargs)

value : scalar, dict, Series, or DataFrame
Value to use to fill holes (e.g. 0), alternately a dict/Series/DataFrame of values specifying which value to use for each index (for a Series) or column (for a DataFrame). (values not in the dict/Series/DataFrame will not be filled). This value cannot be a list.

method : {‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, None}, default None
Method to use for filling holes in reindexed Series pad / ffill: propagate last valid observation forward to next valid backfill / bfill: use NEXT valid observation to fill gap

axis : {0 or ‘index’, 1 or ‘columns’}
inplace : boolean, default False
If True, fill in place. Note: this will modify any other views on this object, (e.g. a no-copy slice for a column in a DataFrame).

limit : int, default None
If method is specified, this is the maximum number of consecutive NaN values to forward/backward fill. In other words, if there is a gap with more than this number of consecutive NaNs, it will only be partially filled. If method is not specified, this is the maximum number of entries along the entire axis where NaNs will be filled. Must be greater than 0 if not None.

downcast : dict, default is None
a dict of item->dtype of what to downcast if possible, or the string ‘infer’ which will try to downcast to an appropriate equal type (e.g. float64 to int64 if possible)

filled : DataFrame

Pandas.DataFrame.fillna() funtion example :

import pandas as pd df = pd.DataFrame(data={'a':[1,2,3,None], 'b':[4,5,None,6]}) print(df) df.fillna(value=0, inplace=True) print(df)


     a    b
0  1.0  4.0
1  2.0  5.0
2  3.0  NaN
3  NaN  6.0
     a    b
0  1.0  4.0
1  2.0  5.0
2  3.0  0.0
3  0.0  6.0



Leave a Reply

Your email address will not be published. Required fields are marked *