1

For example, the Gender attribute will be transformed into two attributes, "Genre=M" and "Genre=F"enter image description here and i need two columns Male and Female ,assigning binary values corresponding to the presence or not presence of the attribute

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

2 Answers2

1

Method 1: You can make use of pd.get_dummies(colname) which will give you n new columns(where n is number of distinct values of that col) each representing binary flags to represent the value state for each row. Method 2: We can also use df. Colname. map({'M' :0,'F':1}) Method 3: We can use replace command like df. Colname. replace(['M', 'F' ], [1, 0], inplace=True) First method is onehot encoding other 2 is similar to label encoding

0

Use the pandas function get_dummies.

get_dummies: Convert categorical variable into dummy/indicator variables. Source.

Example of usage:

s = pd.Series(list('abca'))

pd.get_dummies(s)

Output:

   a  b  c
0  1  0  0
1  0  1  0
2  0  0  1
3  1  0  0
Lucas Araújo
  • 429
  • 5
  • 17