Title: Convert A Numeric Categorical Variable With Patsy
Slug: pandas_convert_numeric_categorical_to_numeric_with_patsy
Summary: Convert A Numeric Categorical Variable With Patsy
Date: 2016-05-01 12:00
Category: Python
Tags: Data Wrangling
Authors: Chris Albon
In [1]:
    
# import modules
import pandas as pd
import patsy
    
In [2]:
    
# Create dataframe
raw_data = {'countrycode': [1, 2, 3, 2, 1]} 
df = pd.DataFrame(raw_data, columns = ['countrycode'])
df
    
    Out[2]:
In [3]:
    
# Convert the countrycode variable into three binary variables
patsy.dmatrix('C(countrycode)-1', df, return_type='dataframe')
    
    Out[3]: