Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert commas decimal separators to dots within a Dataframe

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?

like image 928
Nautilius Avatar asked Jul 29 '15 12:07

Nautilius


People also ask

How do you change a decimal separator to a dot?

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.

How do I change a comma to a dot in CSV?

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!

How do I change a comma to a dot in Python?

str. replace(',', '. ') replaces the comma for a dot.


1 Answers

pandas.read_csv has a decimal parameter for this: doc

I.e. try with:

df = pd.read_csv(Input, delimiter=";", decimal=",") 
like image 113
stellasia Avatar answered Sep 22 '22 14:09

stellasia