Search code examples
pythonpivot-tableimputation

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


I need required imputation in Python:

enter image description here

I tried using:

 # 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]))

but I'm facing error while creating pivot table itself, I'm using Python 3.X latest version.

Looking for other options also?


Solution

  • I believe you can use 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