Pandas DataFrame.to_clipboard — Copy the object to the system clipboard.

Pandas DataFrame.to_clipboard — Copy object to the system clipboard.

The to_clipboard() function copies object to our system’s clipboard. The item is converted to text, which is then copied to the clipboard. The translated text can then be pasted into an Excel spreadsheet or Notepad++.

Syntax of Pandas to_clipboard()

DataFrame.to_clipboard(excel=True, sep=None)

Parameters

ParameterDescriptionRequired/Optional
excelA boolean that has a default value of true allows the output to be copied in excel format. false indicates that write a string to the clipboard representing the object.Required
sepseparator with the default value, '\t'Optional
to_clipboard syntax parameters

Return Value

There is no return value.

Examples of to_clipboard()

import pandas as pd
df=pd.read_csv(r"C:/Users/DASHRATH ASUTKAR/Desktop/Journaldev/pandas/food_sale_data.csv")
print(df)

Note : To download below CSV file Click here.

After importing the pandas library we make sure to save a CSV file containing the data in the same directory as the code file and use the read_csv() function to read the data present in it and later display it via print.

Csv Data

In order to restrict the number of rows to be displayed we use set_options() function

pd.set_option('display.max_rows', 2)
   OrderDate Region         City Category Product  Quantity  UnitPrice  TotalPrice
0   01-01-2020   East       Boston     Bars  Carrot        33       1.77       58.41
..         ...    ...          ...      ...     ...       ...        ...         ...
18  24-02-2020   West  Los Angeles     Bars    Bran        42       1.87       78.54

[19 rows x 8 columns

Example 1: Coping in non-excel format

df.to_clipboard(excel = False, sep = ', ')

After setting the value of excel=false the data or CSV file is copied to the system clipboard which we can later paste into notepad.

Non Excel Format

Example 2: Copying in excel format

df.to_clipboard(excel = True,sep=',')

The default value for excel is true thus making it easy to paste into excel.

Excel Format 1

Conclusion

Excel is used to store big data for analysis in this article we learned a convenient approach to copy a CSV file to our system clipboard which can further be pasted in the desired format.

Reference

Download dummy excel data here
https://www.educative.io/answers/what-is-toclipboard-in-pandas