user3486773
user3486773

Reputation: 1246

How to convert string True / False Pandas columns to int based on column index?

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.

Upvotes: 1

Views: 248

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

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)

Upvotes: 1

Related Questions