change only numeric values to binary in dataframe

I would like to change my dataframe with values into binary,

given df:

summary word1 word2
xyz 0 56
abc 32 0
.. .. ..

I would like to convert ONLY NUMERIC values to binary, meaning – if the value in word1/2 etc is grater than 0 -> 1 and when it’s 0 = stays 0.

category summary word1 word2
category1 xyz 0 1
category2 abc 1 0
.. .. ..

>Solution :

Check if the values in your columns ‘word’ are greater than 0 and convert to int

(df[['word1','word2']] > 0)

   word1  word2
0  False   True
1   True  False

(df[['word1','word2']] > 0).astype(int)

   word1  word2
0      0      1
1      1      0

And assign back:

df[['word1','word2']] = (df[['word1','word2']] > 0).astype(int)

Leave a Reply