text_plain_output_1.png
import numpy as np

import pandas as pd

df = pd.read_csv('../input/california-housing-prices/housing.csv')
df1 = pd.DataFrame()
for location in df['ocean_proximity'].unique():
    df1 = df1.append(df.loc[df['ocean_proximity'] == location][:4])
df1 = df1.reset_index()
df = df1.drop(columns='index')
df

for i, c in zip(df.index, df.columns[:-1]):
    df.at[i, c] = df.at[i + len(df.columns) - 1, c] = np.nan
df

class Imputer_p:
    def __init__(self, strategy_='median', f=1):
        """
        Imputer_p (Pandas):
        strategy_:{"median", tbd} default=’median’
        Specifies the strategy to calucate the statistics for replacing NaNs
        f: int, default=1
        Factor to multiply the first column
        """
        self.strategy_ = strategy_
        self.factor = f
    def fit(self, df):
        if self.strategy_ == 'median':
            self.statistics_ = df.median()
    def transform(self, df_):
        df = df_.copy()
        if self.strategy_ == 'median':
            for column_ in df.columns:
                df.loc[df[column_].isna(), column_] = self.statistics_[column_]
        elif self.strategy_ == 'mult':
            df[df.columns[0]] *= self.factor
        return df

imput_p = Imputer_p()
imput_p.fit(df)
p1 = imput_p.transform(df[df.columns[:-1]])
p1