In the following DataFrame
, the values (1,531.20,...) are imported in the format object
.
I try to convert them to the type float
, applying the method .astype(float)
.
# Create DF from dict of lists/ndarrays
df = pd.DataFrame({'MSCI' : ['1,448.94', '1,480.57', '1,495.18', '1,531.20', '1,532.28'],
'Date' : ['2012-07-09','2012-07-10', '2012-07-11', '2012-07-12', '2012-07-13']})
df.set_index("Date", inplace = True)
df["MSCI"] = df.MSCI.astype(float)
df.info()
It returns me the error:
ValueError: could not convert string to float: '1,448.94'
I will appreciate suggestions to correct this error-
On the other hand, could I somehow get these numbers to be converted to the format on import float
?
In the import I use pd.read_csv
.
The problem is that the values you have are not
float
, if you look closely they have "two commas",1,448.94
.If we assume that the comma is really a thousand separator, we could take all the values, remove the comma and convert them to
float
,So now you have a list with all the values of
dataframe
the float type.I hope I have been of help, anything you tell us!