'How to convert a column in dataframe to only 0 or 1?
Suppose I have a dataset with several rows and columns, and I want a column to be displayed only in the form of 0s and 1s. That particular column does not contain categorical values, but just float values. So the task is wherever the values start with 0 or 0.34, 0.1, etc. must be represented as 0 and all other starting with 1 or 1.x, etc. will be represented as 1. Can anyone of you please help me with this?
Solution 1:[1]
The dumb/straight forward way would be to change the values of the columns from float to int.
int(0.99)
... 0
int(1.53)
... 1
Of course, this will also make 2.10 into 2.
If you can define your criteria, use a lambda function.
df[col] = df[col].apply(lambda x : 0 if x<1 else 1)
Solution 2:[2]
You can use the math's floor function
import math
df[col] = df[col].apply(math.floor)
-> math is a python build in library
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hteza |
| Solution 2 | Alonso G. |
