HilaD
HilaD

Reputation: 881

binarization of numbers python

I have feature that is a number and I want to catgorize all the possibility and making it a binary feature.

feature:

   FEATURE1
1  23
3  20
4  23
5  1
7  8
8  23

wanted input:

   FEATURE1   23   20 ....
1  23         1    0  .... 
3  20         0    1  ....
4  23         1    0  ....
5  1          0    0  ....
7  8          0    0  ....
8  23         1    0  ....

can you help?

Upvotes: 0

Views: 176

Answers (2)

yoav_aaa
yoav_aaa

Reputation: 387

Another way is using pandas pivot_table

df = pd.DataFrame({'FEATURE1': ['10', '13', '13', '22', '15']})  
df.pivot_table(index=df.index, columns='FEATURE1', aggfunc=len          
          ).fillna(value=0)

Upvotes: 1

Alex Ozerov
Alex Ozerov

Reputation: 1028

Try pd.get_dummies() function in pandas.

import pandas as pd
df = pd.DataFrame({'X': ['a', 'b', 'c', 'a']})
df = df.join(pd.get_dummies(df['X']))
print(df)

output:

   X  a  b  c
0  a  1  0  0
1  b  0  1  0
2  c  0  0  1
3  a  1  0  0

Upvotes: 2

Related Questions