Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force all strings to floats? [duplicate]

I have a small dataframe, consisting of just two columns, which should have all floats in it. So, I have two fields name 'Price' and 'Score'. When I look at the data, it all looks like floats to me, but apparently something is a string. Is there some way to kick out these things that are strings, but look like floats? Or, is there a way to force everything to be float? The error occurs on the last line show here, and then nothing else works.

df = pd.read_csv('C:\\my_path\\analytics.csv')
print('done!')
modDF = df[['Price', 'Score']].copy()
modDF = modDF[:100]
for i_dataset, dataset in enumerate(datasets):
    X, y = dataset
    # normalize dataset for easier parameter selection
    X = StandardScaler().fit_transform(X)

Here is the Stack Trace:

datasets = [modDF]
for i_dataset, dataset in enumerate(datasets):
    X, y = dataset
    # normalize dataset for easier parameter selection
    X = StandardScaler().fit_transform(X)


Traceback (most recent call last):

  File "<ipython-input-18-013c2a6bef49>", line 5, in <module>
    X = StandardScaler().fit_transform(X)

  File "C:\Users\rs\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\base.py", line 553, in fit_transform
    return self.fit(X, **fit_params).transform(X)

  File "C:\Users\rs\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\preprocessing\data.py", line 639, in fit
    return self.partial_fit(X, y)

  File "C:\Users\rs\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\preprocessing\data.py", line 663, in partial_fit
    force_all_finite='allow-nan')

  File "C:\Users\rs\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\utils\validation.py", line 496, in check_array
    array = np.asarray(array, dtype=dtype, order=order)

  File "C:\Users\rs\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\core\numeric.py", line 538, in asarray
    return array(a, dtype, copy=False, order=order)

ValueError: could not convert string to float: 'Price'
like image 535
ASH Avatar asked Jan 14 '20 18:01

ASH


1 Answers

You could try using pd.to_numeric like so:

df = df.apply(pd.to_numeric, errors='coerce', downcast='float')

Which would try to convert your data to float, and the data that is not float will be returned as Nan.

Then

df.dropna(how='any', axis=0, inplace=True)

which just drops any rows with at least 1 Nan value in it.

like image 166
Edeki Okoh Avatar answered Oct 22 '22 09:10

Edeki Okoh