how to fill nan values in pandas with mode code example

Example 1: fill na with mode and mean python

cateogry_columns=df.select_dtypes(include=['object']).columns.tolist()
integer_columns=df.select_dtypes(include=['int64','float64']).columns.tolist()

for column in df:
    if df[column].isnull().any():
        if(column in cateogry_columns):
            df[column]=df[column].fillna(df[column].mode()[0])
        else:
            df[column]=df[column].fillna(df[column].mean)

Example 2: how to fill nan values with mean in pandas

df.fillna(df.mean())

Example 3: represent NaN with pandas in python

import pandas as pd

if pd.isnull(float("Nan")):
  print("Null Value.")