Python Forum
Convert Strings to Floats
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert Strings to Floats
#1
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?
Reply
#2
Floats don't deal with ","s, as far as I know.
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert a list of string+bytes into a list of strings (python 3) pacscaloupsu 4 10,744 Mar-17-2020, 07:21 AM
Last Post: markfilan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020