0

I'm trying to convert Dtype object to Dtype float64. Please see the below for the df and info before conversion:

df before conversion

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 101 entries, 2012-01-31 to 2020-05-27
Data columns (total 1 columns):
 #   Column                                 Non-Null Count  Dtype 
---  ------                                 --------------  ----- 
 0   MSCI World Index (MXWO) - Index Value  101 non-null    object
dtypes: object(1)

I then applied this line of code to convert the column "MSCI World Index (MXWO) - Index Value" into float64:

MSCI['MSCI World Index (MXWO) - Index Value']=pd.to_numeric(MSCI['MSCI World Index (MXWO) - Index Value'],errors='coerce')

When I call for the df, I get the following result:

df after conversion

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 101 entries, 2012-01-31 to 2020-05-27
Data columns (total 1 columns):
 #   Column                                 Non-Null Count  Dtype  
---  ------                                 --------------  -----  
 0   MSCI World Index (MXWO) - Index Value  0 non-null      float64
dtypes: float64(1)

What changes should I do to the code in order that the values would be displayed correctly so that I can perform calculations using the values?

AATU
  • 87
  • 1
  • 9

2 Answers2

0

I think you need this method, quite handy for more general things

col_name = 'MSCI World Index (MXWO) - Index Value'
df[col_name].str.replace(',', '.', regex=True).astype(float)
Aramakus
  • 1,910
  • 2
  • 11
  • 22
0

Pandas uses the english convention of periods to indicate decimal values. Replace the commas with periods and then convert the column to floats to fix this:

colname = 'MSCI World Index (MXWO) - Index Value'  # just to make the line shorter
MSCI[colname] = MSCI[colname].str.replace(',', '.').astype(float)
jfaccioni
  • 7,099
  • 1
  • 9
  • 25