0

I have a pandas dataframe with some numeric and some categoric columns. I want to create a new column for each value of every categorical column and give that column a value of 1 in every row where that value is true and 0 in every row where that value is false. So the df is something like this -

col1          col2         col3
 A             P            1
 B             P            3
 A             Q            7

expected result is something like this:

col1          col2         col3      A        B       P        Q
 A             P            1        1        0       1        0
 B             P            3        0        1       1        0
 A             Q            7        1        0       0        1

Is this possible? can someone please help me?

user18334254
  • 227
  • 1
  • 11

1 Answers1

1

Use df.select_dtypes, pd.get_dummies with pd.concat:

# First select all columns which have object dtypes
In [826]: categorical_cols = df.select_dtypes('object').columns    

# Create one-hot encoding for the above cols and concat with df
In [817]: out = pd.concat([df, pd.get_dummies(df[categorical_cols])], 1)
   
In [818]: out
Out[818]: 
  col1 col2  col3  col1_A  col1_B  col2_P  col2_Q
0    A    P     1       1       0       1       0
1    B    P     3       0       1       1       0
2    A    Q     7       1       0       0       1
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58