if your data is a pandas DataFrame, then you can simply call get_dummies. Assume that your data frame is df, and you want to have one binary variable per level of variable 'key'. You can simply call:

pd.get_dummies(df['key'])

and then delete one of the dummy variables, to avoid the multi-colinearity problem. I hope this helps ...

Answer from rezakhorshidi on Stack Overflow
🌐
Practical Business Python
pbpython.com › categorical-encoding.html
Guide to Encoding Categorical Values in Python - Practical Business Python
make object fuel_type object aspiration object num_doors int64 body_style category drive_wheels object engine_location object engine_type object num_cylinders int64 fuel_system object dtype: object · Then you can assign the encoded variable to a new column using the cat.codes accessor:
🌐
MachineLearningMastery
machinelearningmastery.com › home › blog › 3 ways to encode categorical variables for deep learning
3 Ways to Encode Categorical Variables for Deep Learning - MachineLearningMastery.com
August 26, 2020 - As far as I know, the summary of encoding could be: when applying “OrdinalEncoder” we convert categorical labels to integers and, the 9 original fe · Welcome! I'm Jason Brownlee PhD and I help developers get results with machine learning. Read more · Your First Deep Learning Project in Python with Keras Step-by-Step
🌐
CodeSignal
codesignal.com › learn › courses › cleaning-and-transforming-data-with-pandas › lessons › encoding-categorical-variables-using-python
Encoding Categorical Variables Using Python
Encoding using map: We use the map method to replace each label in the Gender column based on our specified dictionary: {'Male': 1, 'Female': 0}. This dictionary tells Python to encode Male as 1 and Female as 0. Adding a new column: The new column Gender_Encoded is created and added to the DataFrame. This column contains the numerical representation of the Gender column, which can now be used for further analysis or as input into a machine learning algorithm. In this lesson, we explored the importance and different methods of encoding categorical variables, with a specific focus on using dictionary mapping in Python.
🌐
DataCamp
datacamp.com › datalab › templates › recipe-python-encoding-categorical-variables
Free Template: Encoding Categorical Variables | DataLab
Use feature engineering techniques such as one-hot encoding and label encoding to pre-process categorical data for use in machine learning algorithms. Pythondata preparation · Use Free Template · Use Free Template · Join 12,000+ Premium members ...
Top answer
1 of 3
31

if your data is a pandas DataFrame, then you can simply call get_dummies. Assume that your data frame is df, and you want to have one binary variable per level of variable 'key'. You can simply call:

pd.get_dummies(df['key'])

and then delete one of the dummy variables, to avoid the multi-colinearity problem. I hope this helps ...

2 of 3
16

The basic method is

import numpy as np
import pandas as pd, os
from sklearn.feature_extraction import DictVectorizer

def one_hot_dataframe(data, cols, replace=False):
    vec = DictVectorizer()
    mkdict = lambda row: dict((col, row[col]) for col in cols)
    vecData = pd.DataFrame(vec.fit_transform(data[cols].apply(mkdict, axis=1)).toarray())
    vecData.columns = vec.get_feature_names()
    vecData.index = data.index
    if replace is True:
        data = data.drop(cols, axis=1)
        data = data.join(vecData)
    return (data, vecData, vec)

data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],
        'year': [2000, 2001, 2002, 2001, 2002],
        'pop': [1.5, 1.7, 3.6, 2.4, 2.9]}

df = pd.DataFrame(data)

df2, _, _ = one_hot_dataframe(df, ['state'], replace=True)
print df2

Here is how to do in sparse format

import numpy as np
import pandas as pd, os
import scipy.sparse as sps
import itertools

def one_hot_column(df, cols, vocabs):
    mats = []; df2 = df.drop(cols,axis=1)
    mats.append(sps.lil_matrix(np.array(df2)))
    for i,col in enumerate(cols):
        mat = sps.lil_matrix((len(df), len(vocabs[i])))
        for j,val in enumerate(np.array(df[col])):
            mat[j,vocabs[i][val]] = 1.
        mats.append(mat)

    res = sps.hstack(mats)   
    return res

data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],
        'year': ['2000', '2001', '2002', '2001', '2002'],
        'pop': [1.5, 1.7, 3.6, 2.4, 2.9]}

df = pd.DataFrame(data)
print df

vocabs = []
vals = ['Ohio','Nevada']
vocabs.append(dict(itertools.izip(vals,range(len(vals)))))
vals = ['2000','2001','2002']
vocabs.append(dict(itertools.izip(vals,range(len(vals)))))

print vocabs

print one_hot_column(df, ['state','year'], vocabs).todense()
🌐
Analytics Vidhya
analyticsvidhya.com › home › what are categorical data encoding methods | binary encoding
What are Categorical Data Encoding Methods | Binary Encoding
May 1, 2025 - By understanding the nature of ... and utilized by their models. For encoding categorical data, we have a python package category encoders....
🌐
Scikit-learn course
inria.github.io › scikit-learn-mooc › python_scripts › 03_categorical_pipeline.html
Encoding of categorical variables — Scikit-learn course
However, be careful when applying this encoding strategy: using this integer representation leads downstream predictive models to assume that the values are ordered (0 < 1 < 2 < 3… for instance). By default, OrdinalEncoder uses a lexicographical strategy to map string category labels to integers. This strategy is arbitrary and often meaningless. For instance, suppose the dataset has a categorical variable named "size" with categories such as “S”, “M”, “L”, “XL”. We would like the integer representation to respect the meaning of the sizes by mapping them to increasing integers such as 0, 1, 2, 3.
🌐
DataCamp
datacamp.com › tutorial › categorical-data
Handling Machine Learning Categorical Data with Python Tutorial | DataCamp
February 23, 2023 - One way to achieve this in pandas is by using the `pd.get_dummies()` method. It is a function in the Pandas library that can be used to perform one-hot encoding on categorical variables in a DataFrame. It takes a DataFrame and returns a new DataFrame with binary columns for each category.
Find elsewhere
🌐
Medium
medium.com › @jaberi.mohamedhabib › encoding-categorical-variables-methods-and-techniques-in-pandas-scikit-learn-and-using-dummy-216ae2d5128d
Encoding Categorical Variables: Methods and Techniques in Pandas, Scikit-learn, and Using Dummy Function | by JABERI Mohamed Habib | Medium
September 27, 2024 - One-Hot Encoding converts each unique category into a new column (binary vector), with each row marked as 1 in the column corresponding to the category it belongs to and 0 in all other columns. This is suitable for nominal categorical variables.
🌐
Medium
niitdigital.medium.com › guide-to-encoding-categorical-values-in-python-eb91cee705d9
Guide to Encoding Categorical Values in Python | by NIIT Digital | Medium
August 4, 2021 - The number of dummy variables depends on the number of variables in the given category. After this, we have a number that is a dummy variable for each category of color. Let’s implement this on python. ... In this categorical data encoding method, the categorical values or variables are transformed into dummy variables.
🌐
GeeksforGeeks
geeksforgeeks.org › machine learning › categorical-data-encoding-techniques-in-machine-learning
Categorical Data Encoding Techniques in Machine Learning - GeeksforGeeks
September 18, 2025 - In this case, each color is encoded based on the mean of the target variable. For instance, 'Red' has a mean target value of approximately 0.485, which reflects the target values for the rows where 'Red' appears. Binary encoding represents categories as binary codes and splits them across multiple columns.
🌐
Medium
medium.com › @favourphilic › simple-guide-to-encoding-categorical-data-in-python-6fa517150350
Simple guide to encoding categorical data in python. | by Victor Jokanola | Medium
June 21, 2022 - Before transforming our dataset into numerical type, it is important to declare our variable as either a feature vector or target variable (which in this case is the class variable). Don’t forget that the type of category encoder to use on the target categorical class is LABEL ENCODER.
🌐
Towards Data Science
towardsdatascience.com › home › data science › encoding categorical variables: a deep dive into target encoding
Encoding Categorical Variables: A Deep Dive into Target Encoding | Towards Data Science
February 5, 2024 - The "categories" parameter determines which columns in the input data should be considered as categorical variables for target encoding. It is Set by default to 'auto' to automatically identify categorical columns during the fitting process.
🌐
Medium
medium.com › @rgr5882 › day-36-data-encoding-for-categorical-variables-fa8b2d40c0b0
Day 36 — Data Encoding for Categorical Variables | by Ricardo García Ramírez | Medium
October 8, 2024 - In today’s post, we explored how to encode categorical variables using Label Encoding and One-Hot Encoding in Python.
🌐
Trainindata
feature-engine.trainindata.com › en › 1.7.x › user_guide › encoding
Categorical Encoding — 1.7.0
These methods are supported by the Python library category encoders. For the time being, we decided not to support these transformations because they return features that are not easy to interpret. And hence, it is very hard to make sense of the outputs of machine learning models trained on categorical variables ...
Starred by 11 users
Forked by 6 users
Languages: Jupyter Notebook
🌐
GeeksforGeeks
geeksforgeeks.org › machine learning › encoding-categorical-data-in-sklearn
Encoding Categorical Data in Sklearn - GeeksforGeeks
September 17, 2025 - Always use the same encoder objects on train and test data to ensure consistency. For categorical variable exploration and encoding in a deployed or production ML pipeline, prefer maintaining category order explicitly for any ordinal features.
🌐
Medium
garg-shelvi.medium.com › category-encoders-c2a9bb192f0a
How to Encode Categorical Data | by Shelvi Garg | Medium
July 12, 2022 - In label encoding, each category is assigned a value from 1 through N where N is the number of categories for the feature. There is no relation or order between these assignments. from sklearn.preprocessing import LabelEncoder le = LabelEncoder() ... Ordinal encoding’s encoded variables retain the ordinal(ordered) nature of the variable.
🌐
Kaggle
kaggle.com › code › paulrohan2020 › tutorial-encoding-categorical-variables
Tutorial-Encoding-Categorical-Variables | Kaggle
April 19, 2022 - Explore and run AI code with Kaggle Notebooks | Using data from Breast cancer data