Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Dataframe.to_csv decimal=',' doesn't work

Tags:

python

pandas

csv

In Python, I'm writing my Pandas Dataframe to a csv file and want to change the decimal delimiter to a comma (,). Like this:

results.to_csv('D:/Data/Kaeashi/BigData/ProcessMining/Voorbeelden/Voorbeeld/CaseEventsCel.csv', sep=';', decimal=',')

But the decimal delimiter in the csv file still is a . Why? What do I do wrong?

like image 440
Jos van der Heijden Avatar asked Jul 13 '16 12:07

Jos van der Heijden


People also ask

What happens when we use PD to_csv ()?

Pandas DataFrame to_csv() function converts DataFrame into CSV data. We can pass a file object to write the CSV data into a file. Otherwise, the CSV data is returned in the string format.

Does pandas to_csv overwrite?

Does pandas To_csv overwrite? If the file already exists, it will be overwritten. If no path is given, then the Frame will be serialized into a string, and that string will be returned.

How do you round off decimal numbers in pandas DataFrame?

round() function is used to round a DataFrame to a variable number of decimal places. This function provides the flexibility to round different columns by different places.


1 Answers

If the decimal parameter doesn't work, maybe it's because the type of the column is object. (check the dtype value in the last line when you do df[column_name])

That can happen if some rows have values that couldn't be parsed as numbers.

You can force the column to change type: Change data type of columns in Pandas. But that can make you lose non numerical data in that column.

like image 175
someone Avatar answered Oct 02 '22 03:10

someone