Skip to content

Instantly share code, notes, and snippets.

@amorgun
amorgun / one-hot.py
Created March 4, 2016 13:35 — forked from ramhiser/one-hot.py
Apply one-hot encoding to a pandas DataFrame
import pandas as pd
import numpy as np
from sklearn.feature_extraction import DictVectorizer
def encode_onehot(df, cols):
"""
One-hot encoding is applied to columns specified in a pandas DataFrame.
Modified from: https://gist.github.com/kljensen/5452382