python - How to convert string into float value in the dataframe -


we facing error when have column have datatype string , value col1 col2 1 .89

so, when using

def azureml_main(dataframe1 = none, dataframe2 = none):      # execution logic goes here     print('input pandas.dataframe #1:')     import pandas pd     import numpy np     sklearn.kernel_approximation import rbfsampler     x =dataframe1.iloc[:,2:1080]     print x     df1 = dataframe1[['colname']]      change = np.array(df1)     b = change.ravel()     print b     rbf_feature = rbfsampler(gamma=1, n_components=100,random_state=1)     print rbf_feature     print "test"     x_features = rbf_feature.fit_transform(x) 

after getting error cannt convert non int type float

use astype(float) e.g.:

df['col'] = df['col'].astype(float) 

or convert_objects:

df = df.convert_objects(convert_numeric=true) 

example:

in [379]:  df = pd.dataframe({'a':['1.23', '0.123']}) df.info() <class 'pandas.core.frame.dataframe'> int64index: 2 entries, 0 1 data columns (total 1 columns):    2 non-null object dtypes: object(1) memory usage: 32.0+ bytes in [380]:  df['a'].astype(float) out[380]: 0    1.230 1    0.123 name: a, dtype: float64  in [382]:  df = df.convert_objects(convert_numeric=true) df.info() <class 'pandas.core.frame.dataframe'> int64index: 2 entries, 0 1 data columns (total 1 columns):    2 non-null float64 dtypes: float64(1) memory usage: 32.0 bytes 

update

if you're running version 0.17.0 or later convert_objects has been replaced methods: to_numeric, to_datetime, , to_timestamp instead of:

df['col'] = df['col'].astype(float) 

you can do:

df['col'] = pd.to_numeric(df['col']) 

note default non convertible values raise error, if want these forced nan do:

df['col'] = pd.to_numeric(df['col'], errors='coerce') 

Comments