1

I am using pandas data frames. The data contains 3032 columns. All the columns are 'object' datatype. How do I convert all the columns to 'float' datatype?

enter image description here

enter image description here

2 Answers 2

3

If need convert integers and floats columns use to_numeric with DataFrame.apply for apply for all columns:

df = df.apply(pd.to_numeric)

working same like:

df = df.apply(lambda x: pd.to_numeric(x))

If some columns contains strings (so converting failed) is possible add errors='coerce' for repalce it to missing values NaN:

df = df.apply(pd.to_numeric, errors='coerce')
Sign up to request clarification or add additional context in comments.

Comments

0

If you are reading the df from a file you can do the same when reading using converters in case you want to apply a customized function, or using dtype to specify the data type you want.

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.