Skip to content Skip to sidebar Skip to footer

Pandas Dataframe Converting Specific Columns From String To Float

I am trying to do some simple analyses on the Kenneth French industry portfolios (first time with Pandas/Python), data is in txt format (see link in the code). Before I can do comp

Solution 1:

I would try the following to force convert everything into floats:

df2=df2.astype(float)

Solution 2:

You can convert specific column to float(or any numerical type for that matter) by

df["column_name"] = pd.to_numeric(df["column_name"])

Posting this because pandas.convert_objects is deprecated in pandas 0.20.1

Solution 3:

You need to assign the result of convert_objects as there is no inplace param:

df2=df2.convert_objects(convert_numeric=True)

you refer to the rename method but that one has an inplace param which you set to True.

Most operations in pandas return a copy and some have inplace param, convert_objects is one that does not. This is probably because if the conversion fails then you don't want to blat over your data with NaNs.

Also the deprecation warning is to split out the different conversion routines, presumably so you can specialise the params e.g. format string for datetime etc..

Post a Comment for "Pandas Dataframe Converting Specific Columns From String To Float"