Python - 模式插补 - 将一列的模式应用于另一列

Python - Mode Imputation - Apply mode for one column on another

我需要在 Python 中进行必要的插补:

我尝试使用:

 # Outlet_Size - Imputation - Its Not Running need to check Version 2.X
    #Import mode function:
    from scipy.stats import mode

    #Determing the mode for each
    outlet_size_mode = data.pivot_table(values='Outlet_Size',
columns='Outlet_Type',aggfunc=(lambda x:mode(x).mode[0]))

但我在创建枢轴 table 本身时遇到错误,我使用的是 Python 3.X 最新版本。

也在寻找其他选择吗?

我相信你可以使用 Series.mode and for select first value add Series.iat:

outlet_size_mode = data.pivot_table(values='Outlet_Size',
                                   columns='Outlet_Type',
                                   aggfunc=lambda x: x.mode().iat[0])
print (outlet_size_mode)
Outlet_Type Supermarket_Type2 Supermarket_Typel
Outlet_Size             Small            Medium