Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ValueError when try to convert a negative number from csv file to integer

Hello i have the csv file below, imported with pandas data = pd.read_csv("1.csv"):

x1,x2,xb,y
−2,1,1,1

I need to convert the negative number (-2) to integer with int(), but i get ValueError:

print(data.iloc[1-1]['x1']) 
> -2 # str

print(int(data.iloc[1-1]['x1']))
> ValueError: invalid literal for int() with base 10: '−2`

I haven’t the error when try to convert positive number:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

print(data.iloc[1-1]['x2'])
> 1 # str
print(int(data.iloc[1-1]['x2']))
> 1 # int

>Solution :

The problem is that many unicode characters look like a minus sign…

The character that you are showing in your question is U+2212 MINUS SIGN. The character that is used for negative numbers is the ASCII U+002D HYPHEN-MINUS. While the print the same, they are different characters. You will have to clean up your data file…

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading