Search code examples
pythonpandasscikit-learnvectorizationone-hot-encoding

one-hot encoding on multi-dimension arrays, using pandas or scikit-learn


I am trying to encode one-hot for my data frame. It is a multi dimension array and I am not sure how to do this. The data frame may look like this:

df = pd.DataFrame({'menu': [['Italian', 'Greek'], ['Japanese'], ['Italian','Greek', 'Japanese']], 'price': ['$$', '$$', '$'], 'location': [['NY', 'CA','MI'], 'CA', ['NY', 'CA','MA']]})

enter image description here

The output I want is something like this:

df2 = pd.DataFrame({'menu': [[1,1,0], [0,0,1], [1,1,1]], 'price': [[1,0], [1,0], [0,1]], 'location': [[1,1,1,0], [0,1,0,0], [1,1,0,1]]})

enter image description here

I am not sure how this can be done using pd.get_dummies or scikit-learn. Can someone help me?


Solution

  • You can use:

    #create list with one item values
    df = df.applymap(lambda x: x if isinstance(x, list) else [x])
    print (df)
           location                        menu price
    0  [NY, CA, MI]            [Italian, Greek]  [$$]
    1          [CA]                  [Japanese]  [$$]
    2  [NY, CA, MA]  [Italian, Greek, Japanese]   [$]
    
    from sklearn.preprocessing import MultiLabelBinarizer
    
    mlb = MultiLabelBinarizer()
    #create Series for each column by list comprehension
    vals = [pd.Series(mlb.fit_transform(df[x]).tolist()) for x in df.columns]
    #concat to df
    df2 = pd.concat(vals, keys=df.columns, axis=1)
    print (df2)
    
           location       menu   price
    0  [1, 0, 1, 1]  [1, 1, 0]  [0, 1]
    1  [1, 0, 0, 0]  [0, 0, 1]  [0, 1]
    2  [1, 1, 0, 1]  [1, 1, 1]  [1, 0]