1

I have a very large dataframe where only the first two columns are not bools. However everything is brought in as a string due to the source. The True/False fields do contain actual blanks (not nan) as well and are spelled out 'True' and 'False'

I'm trying to come up with a dynamic-ish way to do this without typing out or listing every column.

  ndf.iloc[:,2:].astype(bool)

That seems to at least run and change it to bool, but when I add 'inplace=True' it has no effect at storing the property types. I've also tried the code below to no luck. It runs but doesn't actually do anything that I can tell.

  ndf.iloc[:,2:] = ndf.iloc[:,2:].astype(bool) 

I need to be able to write this table back into a database as 0s and 1s ultimately. I'm not the most versed at bools and am hoping there is an easy one liner way to do this that I don't know yet.

1 Answer 1

1

Actually

ndf.iloc[:,2:] = ndf.iloc[:,2:].astype(bool) 

should work and change your data from str/object to bool. It's just you get the same print out with 'True' and True. Check with ndf.dtypes to see the changes after that command.

If you want the booleans as 0 and 1, try:

ndf.iloc[:,2:] = ndf.iloc[:,2:].astype(bool).astype(int)
Sign up to request clarification or add additional context in comments.

1 Comment

That is essentially what I had in my 2nd attempt that didn't appear to work with looking at dtypes, but it does appear that it worked and I missed it. Thanks I will accept this answer when it lets me.

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.