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
Asked
Active
Viewed 295 times
1
-
Basically, [pandas get_dummies](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.get_dummies.html) function will do what you want. – m13op22 Feb 22 '19 at 17:10
-
Thanks peeps ..very helpfull – Akash Meghani Feb 22 '19 at 19:06
2 Answers
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

Sundara Kesavan
- 149
- 8
-
-
You are welcome :) Please do accept answer if its helpful to you. It helps to your own reputation. Thanks in advance – Sundara Kesavan Feb 25 '19 at 04:07
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