Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas: convert multiple categories to dummies

Tags:

python

pandas

I have a table where each row can belong to multiple categories such as,

test = pd.DataFrame({
            'name': ['a', 'b'],
            'category': [['cat1', 'cat2'],['cat1', 'cat3']]
    })

How can I convert each category to a dummy variable in such a way that the above table becomes,

test_res = pd.DataFrame({
        'name': ['a', 'b'],
        'cat1': [1, 1],
        'cat2': [1, 0],
        'cat3': [0, 1]
    })

I tried pd.get_dummies(test['category']) but get the following error,

TypeError: unhashable type: 'list'
like image 635
Stereo Avatar asked Oct 24 '16 07:10

Stereo


People also ask

How do you convert multiple categorical variables into dummy variables?

To convert your categorical variables to dummy variables in Python you c an use Pandas get_dummies() method. For example, if you have the categorical variable “Gender” in your dataframe called “df” you can use the following code to make dummy variables: df_dc = pd. get_dummies(df, columns=['Gender']) .

What does Get_dummies do in pandas?

get_dummies() is used for data manipulation. It converts categorical data into dummy or indicator variables.

What does Drop_first do in Get_dummies?

drop_first. The drop_first parameter specifies whether or not you want to drop the first category of the categorical variable you're encoding. By default, this is set to drop_first = False . This will cause get_dummies to create one dummy variable for every level of the input categorical variable.


1 Answers

You can use pandas.get_dummies, but first convert list column to new DataFrame:

print (pd.DataFrame(test.category.values.tolist()))
      0     1
0  cat1  cat2
1  cat1  cat3

print (pd.get_dummies(pd.DataFrame(test.category.values.tolist()), prefix_sep='', prefix=''))
   cat1  cat2  cat3
0     1     1     0
1     1     0     1

Last add column name by concat:

print (pd.concat([pd.get_dummies(pd.DataFrame(test.category.values.tolist()),
                                 prefix_sep='', prefix='' ), 
        test[['name']]], axis=1))
   cat1  cat2  cat3 name
0     1     1     0    a
1     1     0     1    b

Another solution with Series.str.get_dummies:

print (test.category.astype(str).str.strip('[]'))
0    'cat1', 'cat2'
1    'cat1', 'cat3'
Name: category, dtype: object

df = test.category.astype(str).str.strip('[]').str.get_dummies(', ')
df.columns = df.columns.str.strip("'")
print (df)
   cat1  cat2  cat3
0     1     1     0
1     1     0     1

print (pd.concat([df, test[['name']]], axis=1))
   cat1  cat2  cat3 name
0     1     1     0    a
1     1     0     1    b
like image 159
jezrael Avatar answered Oct 21 '22 11:10

jezrael