def fix_missing ( df , col , name , na_dict ) : if is_numeric_dtype ( col ) : if pd . isnull ( col ) . sum ( ) or ( name in na_dict ) : df [ name + '_na' ] = pd . isnull ( col ) filler = na_dict [ name ] if name in na_dict else col . median ( ) df [ name ] = col . fillna ( filler ) na_dict [ name ] = filler return na_dict
python fill the nan values in the dataset using median values of column
Fill missing data in a column of df with the median and add a { name } _na column which specifies if the data was missing . Parameters : ----------- df : The data frame that will be changed . col : The column of data to fix by filling in missing data . name : The name of the new filled column in df . na_dict : A dictionary of values to create na s of and the value to insert . If name is not a key of na_dict the median will fill any missing data . Also if name is not a key of na_dict and there is no missing data in col then no { name } _na column is not created . Examples : --------- >>> df = pd . DataFrame ( { col1 : [ 1 np . NaN 3 ] col2 : [ 5 2 2 ] } ) >>> df col1 col2 0 1 5 1 nan 2 2 3 2 >>> fix_missing ( df df [ col1 ] col1 {} ) >>> df col1 col2 col1_na 0 1 5 False 1 2 2 True 2 3 2 False >>> df = pd . DataFrame ( { col1 : [ 1 np . NaN 3 ] col2 : [ 5 2 2 ] } ) >>> df col1 col2 0 1 5 1 nan 2 2 3 2 >>> fix_missing ( df df [ col2 ] col2 {} ) >>> df col1 col2 0 1 5 1 nan 2 2 3 2 >>> df = pd . DataFrame ( { col1 : [ 1 np . NaN 3 ] col2 : [ 5 2 2 ] } ) >>> df col1 col2 0 1 5 1 nan 2 2 3 2 >>> fix_missing ( df df [ col1 ] col1 { col1 : 500 } ) >>> df col1 col2 col1_na 0 1 5 False 1 500 2 True 2 3 2 False
def fix_missing(df, col, name, na_dict):
    """ Fill missing data in a column of df with the median, and add a {name}_na column
    which specifies if the data was missing.
    Parameters:
    -----------
    df: The data frame that will be changed.
    col: The column of data to fix by filling in missing data.
    name: The name of the new filled column in df.
    na_dict: A dictionary of values to create na's of and the value to insert. If
        name is not a key of na_dict the median will fill any missing data. Also
        if name is not a key of na_dict and there is no missing data in col, then
        no {name}_na column is not created.
    Examples:
    ---------
    >>> df = pd.DataFrame({'col1' : [1, np.NaN, 3], 'col2' : [5, 2, 2]})
    >>> df
       col1 col2
    0     1    5
    1   nan    2
    2     3    2
    >>> fix_missing(df, df['col1'], 'col1', {})
    >>> df
       col1 col2 col1_na
    0     1    5   False
    1     2    2    True
    2     3    2   False
    >>> df = pd.DataFrame({'col1' : [1, np.NaN, 3], 'col2' : [5, 2, 2]})
    >>> df
       col1 col2
    0     1    5
    1   nan    2
    2     3    2
    >>> fix_missing(df, df['col2'], 'col2', {})
    >>> df
       col1 col2
    0     1    5
    1   nan    2
    2     3    2
    >>> df = pd.DataFrame({'col1' : [1, np.NaN, 3], 'col2' : [5, 2, 2]})
    >>> df
       col1 col2
    0     1    5
    1   nan    2
    2     3    2
    >>> fix_missing(df, df['col1'], 'col1', {'col1' : 500})
    >>> df
       col1 col2 col1_na
    0     1    5   False
    1   500    2    True
    2     3    2   False
    """
    if is_numeric_dtype(col):
        if pd.isnull(col).sum() or (name in na_dict):
            df[name+'_na'] = pd.isnull(col)
            filler = na_dict[name] if name in na_dict else col.median()
            df[name] = col.fillna(filler)
            na_dict[name] = filler
    return na_dict