3

How can I copy a DataFrame to_clipboard and paste it in excel with commas as decimal?

In R this is simple.

write.table(obj, 'clipboard', dec = ',')

But I cannot figure out in pandas to_clipboard.

I unsuccessfully tried changing:

import locale
locale.setlocale(locale.LC_ALL, '')

Spanish_Argentina.1252

or

df.to_clipboard(float_format = '%,%')
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Diego
  • 2,196
  • 1
  • 21
  • 26
  • 1
    `to_clipboard` takes all the same parameters as `to_csv`. But I think the main thing is that your float format is not correctly specified. You'll likely need to mess with your locale settings: http://stackoverflow.com/questions/6633523/how-can-i-convert-a-string-with-dot-and-comma-into-a-float-number-in-python – Paul H Feb 05 '15 at 18:41
  • @PaulH I thought that `read_csv` used the [format specification](https://docs.python.org/2/library/string.html#format-specification-mini-language) so that `float_format=":n"` would work (if you had set your locale). It doesn't seem to work though (TypeError not all arguments covered). :( – Andy Hayden Feb 05 '15 at 20:24
  • I tried `df.to_clipboard(float_format = ':n')` but shows me `TypeError: 'str' Object is not callable` – Diego Feb 05 '15 at 20:41
  • 1
    There is an open issue to add this functionality (`decimal` keyword) to `to_csv` (and thus `to_clipboard`): https://github.com/pydata/pandas/issues/781 – joris Feb 09 '15 at 09:38
  • @AndyHayden The local does work, but you need to specify the argument as a function, not a string, see my answer below. A bit awkward in any case .. – joris Feb 09 '15 at 10:01
  • The accepted answer is outdated now that [`df.to_clipboard(decimal=',')` is implemented](https://stackoverflow.com/a/50004011/1026). – Nickolay May 26 '18 at 20:04

2 Answers2

8

Since Pandas 0.16 you can use

df.to_clipboard(decimal=',')

to_clipboard() passes extra kwargs to to_csv(), which has other useful options.

Ronan Paixão
  • 8,297
  • 1
  • 31
  • 27
2

There are some different ways to achieve this. First, it is possible with float_format and your locale, although the use is not so straightforward (but simple once you know it: the float_format argument should be a function that can be called):

df.to_clipboard(float_format='{:n}'.format)

A small illustration:

In [97]: df = pd.DataFrame(np.random.randn(5,2), columns=['A', 'B'])

In [98]: df
Out[98]:
          A         B
0  1.125438 -1.015477
1  0.900816  1.283971
2  0.874250  1.058217
3 -0.013020  0.758841
4 -0.030534 -0.395631

In [99]: df.to_clipboard(float_format='{:n}'.format)

gives:

           A         B
0    1,12544  -1,01548
1   0,900816   1,28397
2    0,87425   1,05822
3 -0,0130202  0,758841
4 -0,0305337 -0,395631

If you don't want to rely on the locale setting but still have comma decimal output, you can do this:

class CommaFloatFormatter:
    def __mod__(self, x):
        return str(x).replace('.',',')

df.to_clipboard(float_format=CommaFloatFormatter())

or simply do the conversion before writing the data to clipboard:

df.applymap(lambda x: str(x).replace('.',',')).to_clipboard()
joris
  • 133,120
  • 36
  • 247
  • 202