0

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
.. .. ..
2
  • are your data types numeric? (df.dtypes) Commented Nov 24, 2022 at 14:32
  • no, intigers, as it calculates the occurrences in the summary Commented Nov 24, 2022 at 14:35

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.