Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python replace 1.0 and 0.0 with True and False in dataframe

I have a big data frame. I want to replace float 1.0 and 0.0 with true and false.

My code:

import pandas as pd

df = pd.DataFrame({"A":[1.0,0.0,1.0,0.0]})
df.replace(1.0,True,inplace=True).replace(0.0,False,inplace=True) 

Present output:

AttributeError: 'NoneType' object has no attribute 'replace'

If I do it separately in two lines, it will work. I want to do it in one line.

like image 549
Mainland Avatar asked Dec 06 '25 04:12

Mainland


1 Answers

Try with bool :-)

df.A=df.A.astype(bool)
df
Out[69]: 
       A
0   True
1  False
2   True
3  False
like image 75
BENY Avatar answered Dec 07 '25 20:12

BENY