Python Forum

Full Version: Convert Strings to Floats
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear all.

I have the following dataset:

0 27.05.2020 19,93 19,80 19,93 19,15 74,31M 1,32%
1 26.05.2020 19,67 19,98 20,09 19,33 68,29M 0,98%
2 25.05.2020 19,48 19,48 19,56 19,26 37,58M 4,34%
3 22.05.2020 18,67 18,80 18,90 18,35 68,84M -2,71%
4 21.05.2020 19,19 19,50 19,77 19,07 80,92M -0,57%
5 20.05.2020 19,30 19,09 19,44 19,06 74,12M 3,32%

with following datatype:

# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Data 21 non-null object
1 Ăšltimo 21 non-null object
2 Abertura 21 non-null object
3 Alta 21 non-null object
4 Baixa 21 non-null object
5 Vol. 21 non-null object
6 Var. % 21 non-null object

I am trying to convert column Alta to float without any sucess.

dataset['Alta'] = dataset['Alta'].astype(float)

and errer is :

ValueError: could not convert string to float: '19,93'

CAn you help me please?
Floats don't deal with ","s, as far as I know.
You can try and use str.replace() using lambda function to convert all "," to "." and then try the astype method.Here is an article that describes conversion from Strings to lists and might help you