pandas.core.reshape.get_dummies

pandas.core.reshape.get_dummies(data, prefix=None, prefix_sep='_', dummy_na=False)

Convert categorical variable into dummy/indicator variables

Parameters :

data : array-like or Series

prefix : string, default None

String to append DataFrame column names

prefix_sep : string, default ‘_’

If appending prefix, separator/delimiter to use

dummy_na : bool, default False

Add a column to indicate NaNs, if False NaNs are ignored.

Returns :

dummies : DataFrame

Examples

>>> import pandas as pd
>>> s = pd.Series(list('abca'))
>>> get_dummies(s)
   a  b  c
0  1  0  0
1  0  1  0
2  0  0  1
3  1  0  0
>>> s1 = ['a', 'b', np.nan]
>>> get_dummies(s1)
   a  b
0  1  0
1  0  1
2  0  0
>>> get_dummies(s1, dummy_na=True)
   a  b  NaN
0  1  0    0
1  0  1    0
2  0  0    1

See also Series.str.get_dummies.