w3resource

Pandas Series: to_clipboard() function

Series-to_clipboard() function

The to_clipboard() function is used to copy object to the system clipboard.

Write a text representation of object to the system clipboard. This can be pasted into Excel, for example.

Syntax:

Series.to_clipboard(self, excel=True, sep=None, **kwargs)

Parameters:

Name Description Type/Default Value Required / Optional
excel Indication of expected JSON string format.
  • True, use the provided separator, writing in a csv format for allowing easy pasting into excel.
  • False, write a string representation of the object to the clipboard.
bool, default True Required
sep Field delimiter. str, default '\t' Required
**kwargs These parameters will be passed to DataFrame.to_csv. Required

Notes: Requirements for your platform.

  • Linux : xclip, or xsel (with PyQt4 modules)
  • Windows : none
  • OS X : none

Example - Copy the contents of a DataFrame to the clipboard:

Python-Pandas Code:

import numpy as np
import pandas as pd
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

Example - We can omit the the index by passing the keyword index and setting it to false:

Python-Pandas Code:

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

Previous: Series-to_json() function
Next: Series-to_latex() function



Follow us on Facebook and Twitter for latest update.