I have a dataframe that looks like this with 13000 rows
print(df)
Date Price Nation
0 01/01/2018 -5.000,73 Spain
1 01/01/2018 15,60 Italy
3 01/01/2018 14,13 Italy
4 01/01/2018 12,53 Spain
5 01/01/2018 16,64 Italy
6 01/01/2018 22,48 Italy
7 01/01/2018 24,30 Italy
8 01/01/2018 24,88 Spain
9 01/01/2018 31,40 Italy
10 01/01/2018 18,74 Italy
Price Column is a non-null object
I removed all the white spaces and dropped al the empty rows
I tried to work only with the price column converting from a Series to a String
string=df['Price'].to_string()
print(string)
0 -5.000,73
1 15,60
3 14,13
4 12,53
5 16,64
6 22.48
7 24.30
8 24.88
9 31.40
10 18.74
When I tried
string=string.strip('.')
string=string.replace(',','.')
float(string.strip().strip("'"))
The system returned me an error message:
ValueError: could not convert string to float: '0
-5.000.73\n1 15.60\n3 14.13\n4 12.53\n5 16.64\n6 22.48\n7 24.30\n8 24.88\n9 31.40\n10
The data are loaded from a csv, and the delimiter is ,
- How can I easily convert to a float, considering that \n7, \n10,..., \n160000 are not regularly increased?
- Why I get this kind of problem?
- If I open my csv with Jupyter or on excel I cannot see
/n32
, but only something like this
"01/01/2018","16,60","Spain"
, I know that/n
is used for giving a vertical space, but I don't know how to deal with it, I noticed that is related with the index
I read this questions, tried other solutions, but none solved my problem:
numpy.loadtxt, ValueError: could not convert string to float
ValueError: could not convert string to float: '-0,274697\n'
How do I parse a string to a float or int in Python?
Python convert string to float