python - ValueError when trying to convert Dataframe Column into float -


i trying convert data under column yield_pct of dataframe df below float types, facing issues doing this. error valueerror: not convert string float: ' na' , hence added line if row1['yield_pct'] != 'na': code below, yet same error after adding line.

              date      maturity      yield_pct currency 0       1986-01-01          0.25             na      cad 1       1986-01-02          0.25   0.0948511020      cad 2       1986-01-03          0.25   0.0972953210      cad 3       1986-01-06          0.25   0.0965403640      cad 4       1986-01-07          0.25   0.0953292440      cad   (i1, row1) in (df.iterrows()):     if row1['yield_pct'] != 'na':         row1['yield_pct'] = float(row1['yield_pct'])         if isinstance(row1['yield_pct'], float)==1:             print('success')         else:             print('failure') 

thank you

edit: lower part of dataframe df:

920538  2015-01-19  empty string                     cad 920539  2015-01-20  empty string                     cad 920540  2015-01-21  empty string                     cad 920541  2015-01-22  empty string                     cad 920542  2015-01-23  empty string                     cad 920543  2015-01-26  empty string                     cad 

code using:

df = update('cad')[0] (i1, row1) in (df.iterrows()):     df = df.convert_objects(convert_numeric=true)     if isinstance(row1['yield_pct'], float)==1:         print('success')     else:         print('failure') 

just use convert_objects, coerce duff values nan:

in [75]: df = df.convert_objects(convert_numeric=true) df  out[75]:          date  maturity  yield_pct currency 0  1986-01-01      0.25        nan      cad 1  1986-01-02      0.25   0.094851      cad 2  1986-01-03      0.25   0.097295      cad 3  1986-01-06      0.25   0.096540      cad 4  1986-01-07      0.25   0.095329      cad 

Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -