Skip to content

Instantly share code, notes, and snippets.

@ramhiser
Last active April 7, 2021 06:44
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save ramhiser/982ce339d5f8c9a769a0 to your computer and use it in GitHub Desktop.
Save ramhiser/982ce339d5f8c9a769a0 to your computer and use it in GitHub Desktop.
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
Details:
http://en.wikipedia.org/wiki/One-hot
http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html
@param df pandas DataFrame
@param cols a list of columns to encode
@return a DataFrame with one-hot encoding
"""
vec = DictVectorizer()
vec_data = pd.DataFrame(vec.fit_transform(df[cols].to_dict(outtype='records')).toarray())
vec_data.columns = vec.get_feature_names()
vec_data.index = df.index
df = df.drop(cols, axis=1)
df = df.join(vec_data)
return df
def main():
np.random.seed(42)
df = pd.DataFrame(np.random.randn(25, 3), columns=['a', 'b', 'c'])
# Make some random categorical columns
df['e'] = [random.choice(('Chicago', 'Boston', 'New York')) for i in range(df.shape[0])]
df['f'] = [random.choice(('Chrome', 'Firefox', 'Opera', "Safari")) for i in range(df.shape[0])]
# Vectorize the categorical columns: e & f
df = encode_onehot(df, cols=['e', 'f'])
print df.head()
if __name__ == '__main__':
main()
@djvine
Copy link

djvine commented Jan 22, 2017

From a machine learning perspective is there a preferred option between get_dummies and factorize. I have read that since factorize produces unequal distances between categorical values, that the vectorized output of get_dummies is preferred.

E.g.:

    red = 0
    blue = 1
    green = 2

    => green = 2* blue

which obviously makes no sense.

@kid1412z
Copy link

kid1412z commented Mar 1, 2017

hi there, I found encode_onehot(df, cols) can only encode columns all of strings. When apply to df = pd.DataFrame({'category':[6,7,8,6,7,8], 'number':[1,2,3,4,5,6]}) the method vec.get_feature_names() will only return ['category'] and the encoding will fail.

@kid1412z
Copy link

kid1412z commented Mar 1, 2017

add the method below will work well on both of the cases above:

def one_hot(df, cols):
    for each in cols:
        dummies = pd.get_dummies(df[each], prefix=each, drop_first=False)
        df = pd.concat([df, dummies], axis=1)
    return df

@suchendra-h
Copy link

What is the best way to use sklearn's OneHotEncoder with pandas dataframe?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment