简体   繁体   中英

Python: Create two dummy columns from one column

I have a one column Pandas dataframe:

'asdf'

0
1
1
1
0
...
1

How do I turn it into:

1    0
0    1
0    1
0    1
1    0

such that the first column is the "0" column and the second column is the "1" column and both are dummy variables?

You can use get_dummies , for example

import numpy as np
import pandas as pd

one = pd.DataFrame({'asdf':np.random.randint(0,2,10)})
two = pd.get_dummies(one.loc[:,'asdf'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM