Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading