Reputation: 47
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?
Upvotes: 1
Views: 54
Reputation: 620
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.
Upvotes: 0
Reputation: 862581
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')
Upvotes: 3