elzell
elzell

Reputation: 2306

Pandas DataFrame to csv: Specifying decimal separator for mixed type

I've found a somewhat strange behaviour when I create a Pandas DataFrame from lists and convert it to csv with a specific decimal separator.

This works as expected:

>>> import pandas as pd
>>> a = pd.DataFrame([['a', 0.1], ['b', 0.2]])
>>> a
   0    1
0  a  0.1
1  b  0.2
>>> a.to_csv(decimal=',', sep=' ')
' 0 1\n0 a 0,1\n1 b 0,2\n'

However, in this case the decimal separator is not set properly:

>>> b = pd.DataFrame([['a', 'b'], [0.1, 0.2]])
>>> b
     0    1
0    a    b
1  0.1  0.2
>>> b.to_csv(decimal=',', sep=' ')
' 0 1\n0 a b\n1 0.1 0.2\n'

When I transpose b in order to get a DataFrame like a the decimal separator is still not properly set:

>>> b.T.to_csv(decimal=',', sep=' ')
' 0 1\n0 a 0.1\n1 b 0.2\n'

Why I am asking: In my program I have columns as individual lists (e.g. col1 = ['a', 'b'] and col2 = [0.1, 0.2], but the number and format of columns can vary) and I would like to convert them to csv with a specific decimal separator, so I'd like to have an output like

' 0 1\n0 a 0,1\n1 b 0,2\n'

Upvotes: 3

Views: 2891

Answers (1)

Nickil Maveli
Nickil Maveli

Reputation: 29719

Use applymap and cast the float typed cells to str by checking explicitly for their type. Then, replace the decimal dot(.) with the comma (,) as each cell now constitutes a string and dump the contents to a csv file later.

b.applymap(lambda x: str(x).replace(".", ",") if isinstance(x, float) else x).to_csv(sep=" ")
# ' 0 1\n0 a b\n1 0,1 0,2\n'

Upvotes: 2

Related Questions