I am importing a CSV file like the one below, using pandas.read_csv
:
df = pd.read_csv(Input, delimiter=";")
Example of CSV file:
10;01.02.2015 16:58;01.02.2015 16:58;-0.59;0.1;-4.39;NotApplicable;0.79;0.2 11;01.02.2015 16:58;01.02.2015 16:58;-0.57;0.2;-2.87;NotApplicable;0.79;0.21
The problem is that when I later on in my code try to use these values I get this error: TypeError: can't multiply sequence by non-int of type 'float'
The error is because the number I'm trying to use is not written with a dot (.
) as a decimal separator but a comma(,
). After manually changing the commas to a dots my program works.
I can't change the format of my input, and thus have to replace the commas in my DataFrame in order for my code to work, and I want python to do this without the need of doing it manually. Do you have any suggestions?
Click File > Options. On the Advanced tab, under Editing options, clear the Use system separators check box. Type new separators in the Decimal separator and Thousands separator boxes. Tip: When you want to use the system separators again, select the Use system separators check box.
Either to replace the comma to a dot - OR - save using a ";" (semicolon) as delimiter. CDD does recognize ";" as delimiter in a CSV file as well!
str. replace(',', '. ') replaces the comma for a dot.
pandas.read_csv
has a decimal
parameter for this: doc
I.e. try with:
df = pd.read_csv(Input, delimiter=";", decimal=",")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With