9951 explained code solutions for 126 technologies


python-scikit-learnUse imputer for one column only


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

im = impute.SimpleImputer(missing_values=np.nan, strategy='mean')

df = pd.DataFrame([[7, 2, 3], [4, np.nan, 6], [10, 5, 9]], columns=['A', 'B', 'C'])
df['B'] = im.fit_transform(df[['B']])ctrl + c
from sklearn import

import module from scikit-learn

import numpy

import Numpy module

import pandas as pd

load Pandas module

.SimpleImputer(

univariate imputer for completing missing values with simple strategies.

df[['B']]

impute values for B column only


Usage example

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

im = impute.SimpleImputer(missing_values=np.nan, strategy='mean')

df = pd.DataFrame([[7, 2, 3], [4, np.nan, 6], [10, 5, 9]], columns=['A', 'B', 'C'])
df['B'] = im.fit_transform(df[['B']])

print(df)
output
    A    B  C
0   7  2.0  3
1   4  3.5  6
2  10  5.0  9