0

I am trying to convert columns from multiple dataframes to boolean. What I have written to convert them is the following:

for i in range(0,4):
        df[i][['Col1','Col2','Col3','Col4']].astype('bool')

However it does not convert anything. Not all the columns from the dataframes need to be converted to boolean, so I have selected above only those ones that need to be converted. When I print df[1].dtypes (but I get the same results also from the other dataframes), all the columns above are objects, not boolean.

Could you please tell me where the error is in my code?

1 Answer 1

1

Note that .astype returns a new object, and does not do the change in place. In order to perform the change, run:

for i in range(0,4):
    df[i][['Col1','Col2','Col3','Col4']] = df[i][['Col1','Col2','Col3','Col4']].astype('bool')
Sign up to request clarification or add additional context in comments.

Comments

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.