8

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?

Andy
  • 49,085
  • 60
  • 166
  • 233
  • 1
    You can check the version of pandas by typing pandas.__version__ in your console. The documantation says that your desired operation is "New in version 0.16.0." Maybe your version does not yet support this? – Ohumeronen Jul 13 '16 at 12:55

5 Answers5

8

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.

someone
  • 449
  • 5
  • 10
1

This functionality wasn't added until 0.16.0

Added decimal option in to_csv to provide formatting for non-‘.’ decimal separators (GH781)

Upgrade pandas to something more recent and it will work. The code below uses the 10 minute tutorial and pandas version 0.18.1

>>> import pandas as pd
>>> import numpy as np
>>> dates = pd.date_range('20130101', periods=6)
>>> df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))
>>> df
                   A         B         C         D
2013-01-01 -0.157833  1.719554  0.564592 -0.228870
2013-01-02 -0.316600  1.545763 -0.206499  0.793412
2013-01-03  1.905803  1.172803  0.744010  1.563306
2013-01-04 -0.142676 -0.362548 -0.554799 -0.086404
2013-01-05  1.708246 -0.505940 -1.135422  0.810446
2013-01-06 -0.150899  0.794215 -0.628903  0.598574
>>> df.to_csv("test.csv", sep=';', decimal=',')

This creates a "test.csv" file that looks like this:

;A;B;C;D
2013-01-01;-0,157833276159;1,71955439009;0,564592278787;-0,228870244247
2013-01-02;-0,316599953358;1,54576303958;-0,206499307398;0,793411528039
2013-01-03;1,90580284184;1,17280324924;0,744010110291;1,56330623177
2013-01-04;-0,142676406494;-0,36254842687;-0,554799190671;-0,0864039782679
2013-01-05;1,70824597265;-0,50594004498;-1,13542154086;0,810446051841
2013-01-06;-0,150899136973;0,794214730009;-0,628902891897;0,598573645748
Andy
  • 49,085
  • 60
  • 166
  • 233
1

In the case when data is an object, and not a plain float type, for example python decimal.Decimal(10.12). First, change a type and then write to CSV file:

import pandas as pd
from decimal import Decimal

data_frame = pd.DataFrame(data={'col1': [1.1, 2.2], 'col2': [Decimal(3.3), Decimal(4.4)]})
data_frame.to_csv('report_decimal_dot.csv', sep=';', decimal=',', float_format='%.2f')

data_frame = data_frame.applymap(lambda x: float(x) if isinstance(x, Decimal) else x)
data_frame.to_csv('report_decimal_comma.csv', sep=';', decimal=',', float_format='%.2f')

tarasinf
  • 796
  • 5
  • 17
0

Somehow i don't get this to work either. I always just end up using the following script to rectify it. It's dirty but it works for my ends:

for col in df.columns:
    try:
        df[col] = df[col].apply(lambda x: float(x.replace('.','').replace(',','.')))
   except:
        pass

EDIT: misread the question, you might use the same tactic the other way around by changing all your floats to strings :). Then again, you should probably just figure out why it's not working. Due post it if you get it to work.

PdevG
  • 3,427
  • 15
  • 30
0

This example suppose to work (as it works for me):

import pandas as pd
import numpy as np

s = pd.Series(np.random.randn(10))
with open('Data/out.csv', 'w') as f:
    s.to_csv(f, index=True, header=True, decimal=',', sep=';', float_format='%.3f')

out.csv:

;0
0;0,091
1;-0,009
2;-1,427
3;0,022
4;-1,270
5;-1,134
6;-0,965
7;-1,298
8;-0,854
9;0,150

I don't see exactly why your code doesn't work, but anyway, try to use the above example to your needs.

Gal Dreiman
  • 3,969
  • 2
  • 21
  • 40