Search code examples
excelexcel-formulaworksheet-function

One-hot (dummy) encoding of categorical data in Excel


How to convert categorical data to one-hot encoded numeric format. Output format is depicted in the figure below: enter image description here

Is it possible to automatically extract data classes and create columns for each class without having to create all columns (White, Red, Black, ...) manually in Excel?


Solution

  • you could do it with formulas

    from your image above you could write in cell E2

     =INDEX($B:$B,COLUMN(H:H)-COLUMN($E:$E))    'DRAG FORMULA ACROSS AS NEEDED
    

    in cell D3 use

    =A3                                         'DRAG FORMULA DOWN AS NEEDED
    

    and in cell E3 use

    =IF($B3=E$2,1,0)                   'DRAG FORMULA ACROSS AND DOWN AS NEEDED