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]:
countrycode
0 1
1 2
2 3
3 2
4 1

In [3]:
# Convert the countrycode variable into three binary variables
patsy.dmatrix('C(countrycode)-1', df, return_type='dataframe')


Out[3]:
C(countrycode)[1] C(countrycode)[2] C(countrycode)[3]
0 1.0 0.0 0.0
1 0.0 1.0 0.0
2 0.0 0.0 1.0
3 0.0 1.0 0.0
4 1.0 0.0 0.0