how to change int to Binary value

Question:

I have a problem changing my data into binary. It’s not so complicated just using basic math; like if a = 60, then the result is "good", and when a >= 60, then it is "very good", but this is implemented in the data below:

Screenshot

This is my data; I want to change new_cases data to be binary value when the data >=1; I want the result to be 1, but when I use

Dt[Dt['new_cases'] >= 1 ] = 1

It doesn’t work.

Please, is anyone able to run it? Any ideas? What could be causing this issue?

Thanks!

Asked By: aditya murti

||

Answers:

You have to specify the column where you want to change the values:
Dt.loc[Dt['new_cases'] >= 1, 'new_cases'] = 1

Answered By: Bence

Use

Dt["new_cases"] = Dt["new_cases"].apply(lambda x: 1 if x >= 1 else 0)

OR

Dt["new_cases"] = 1
Dt.loc[Dt["new_cases"] < 1, "new_cases"] = 0
Answered By: JayPeerachai
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.