how to use pandas.to_clipboard with comma decimal separator

  • Last Update :
  • Techknowledgy :

Since Pandas 0.16 you can use

df.to_clipboard(decimal = ',')

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

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

df.applymap(lambda x: str(x).replace('.', ',')).to_clipboard()

Suggestion : 2

How can I copy a DataFrame to_clipboard and paste it in excel with commas as decimal?,or simply do the conversion before writing the data to clipboard:,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):, [FIXED] Visual studio code: "matplotlib" is not accessed Pylance Issue I get the error in the title when I try to import matplotlib. Please I have been sea...

In R this is simple.

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

I unsuccessfully tried changing:

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

or

df.to_clipboard(float_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

Suggestion : 3

Write a DataFrame to a comma-separated values (csv) file.,Read text from clipboard and pass to read_csv.,Write a text representation of object to the system clipboard. This can be pasted into Excel, for example.,These parameters will be passed to DataFrame.to_csv.

>>> df = pd.DataFrame([
   [1, 2, 3],
   [4, 5, 6]
], columns = ['A', 'B', 'C'])
>>> df.to_clipboard(sep = ',')
   ...# Wrote the following to the system clipboard:
   ...#, A, B, C
   ...# 0, 1, 2, 3
   ...# 1, 4, 5, 6
>>> df.to_clipboard(sep = ',', index = False)
   ...# Wrote the following to the system clipboard:
   ...# A, B, C
   ...# 1, 2, 3
   ...# 4, 5, 6

Suggestion : 4

Character recognized as decimal separator. E.g. use ‘,’ for European data.,If path_or_buf is None, returns the resulting csv format as a string. Otherwise returns None.,File path or object, if None is provided the result is returned as a string. If a file object is passed it should be opened with newline=’‘, disabling universal newlines.,String of length 1. Character used to escape sep and quotechar when appropriate.

Examples

>>> df = pd.DataFrame({
      'name': ['Raphael', 'Donatello'],
      ...'mask': ['red', 'purple'],
      ...'weapon': ['sai', 'bo staff']
   }) >>>
   df.to_csv(index = False)
'name,mask,weapon\nRaphael,red,sai\nDonatello,purple,bo staff\n'