Search code examples
pythonpandasdataframenullfillna

Creating a function that fills null value automatically


My current project has a ton of columns with nulls. I technically can just either fill them or drop them one by one. But I thought, maybe I could write a function that does the fillna for me automatically. Here is what I come up so far,

def fill_null(df, column):
    if np.dtype(df[column]) == 'O':
       df[column].fillna('NA', inplace = True)
    elif np.dtype(df[column]) != 'O':
       df[column].fillna(0, inplace = True) 
    return

It works. But is there a way to make it automatically iterate through the whole dataframe and fill them automatically? or I am asking too much. I am still new to programming, and not that good at iteration.

Thanks!


Solution

  • Use DataFrame.select_dtypes for object columns and create dictionary for replace by DataFrame.fillna, then replace all another columns with 0:

    d = dict.fromkeys(df.select_dtypes(object).columns, 'NA')
    df = df.fillna(d).fillna(0)
    

    Another solution:

    df = df.apply(lambda x: x.fillna('NA') if x.dtype == 'O' else x.fillna(0))