Imputation using central tendency

    0

    0

    Wyx Smrf

    , impute_column

    Library: Pandas

    import pandas as pd
    
    def impute_central_tendency(dataframe, column):
      central_tendency = ["mean", "median", "mode"]
      impute_df = pd.DataFrame()
      impute_column = dataframe[column]
    
      # Impute null values using cental tendency
      for stat in central_tendency:
        if stat == "mode":
          impute_df[stat] = eval(f"impute_column.fillna(value=impute_column.mode()[0])")
        else:
          impute_df[stat] = eval(f"impute_column.fillna(value=impute_column.{stat}())")
    
      # Round all answers to nearest integer
      round_impute = impute_df.round(0).astype(int)
    
      return round_impute
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.