Impute missing data

Imports

import pandas as pd
import numpy as np
from sklearn.impute import SimpleImputer

Create data

d = {'col1': [1, np.nan, 10, 14], 'col2': [3, 4, 5, np.nan]}
df = pd.DataFrame(d)
df

col1 col2
0 1.0 3.0
1 NaN 4.0
2 10.0 5.0
3 14.0 NaN

Initialise imputer

imputer = SimpleImputer(strategy="mean")

Train imputer

# Fit the imputer 
imputer = imputer.fit(df)

Apply imputer

# Apply the imputer
df_imputed = imputer.transform(df)

# View data
df_imputed
array([[ 1.        ,  3.        ],
       [ 8.33333333,  4.        ],
       [10.        ,  5.        ],
       [14.        ,  4.        ]])