2

I am trying to convert dataframe columns from string to int to boolean using a loop. I have confirmed that n does correctly catch the df column names but am unable to reference them in the convert statement.

for n in (names(df))
    df[!,:n] = convert.(Bool, (parse.(Int,df[!,:n])))
end

The error I get is:

ERROR: LoadError: ArgumentError: column name :n not found in the data frame

1
  • 1
    The problem is :n is a symbol and is not referring to n in your loop for n in ..., to refer to n use n not :n, i.e. the second line should be df[!,n] = convert.(Bool, (parse.(Int,df[!,n]))). Commented Feb 23, 2022 at 10:25

1 Answer 1

2

You shouldn't use : before the column names, and parse can parse a string as a Bool too.

transform!(df, names(df) .=> ByRow(x->parse(Bool, x)), renamecols = false)
Sign up to request clarification or add additional context in comments.

1 Comment

or mapcols(x -> parse.(Bool, x), df),

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.