Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have a big data frame. I want to replace float 1.0 and 0.0 with the 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.

1 Answer

0 votes
by (36.8k points)

Try with bool 

df.A=df.A.astype(bool)

df

Out[69]: 

       A

0   True

1  False

2   True

3  False

Do check out Python Data Science Course which helps you understand from scratch  

Browse Categories

...