Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


What is to_clipboard() in pandas?

Salman Yousaf

Take a Free Course

Great engineers are always investing in themselves. Upskill yourself today.


The to_clipboard() function copies objects to our system’s clipboard. It translates the object into its text representation and copies it to the clipboard. You can then paste the converted text into an excel sheet or Notepad++.


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


  1. excel: This is boolean, and the default value is True. This parameter produces the output in a CSV format and makes it easier to paste into excel. True indicates the use of the provided default separator for CSV pasting. False indicates that write a string to the clipboard representing the object.
    1. sep: This is the separator with the default value, '\t'. This means that the data is separated by a tab. It serves as a field delimiter which means we can indicate the ending and start of the data here. It can be a string, a comma, or any other character. 
    2. **kwargs: This is a special keyword that allows us to take a variable-length argument. Then, these parameters will be sent to the DataFrame.to_csv().

    Return value

    None: It does not return any value.


     # import pandas library in program
    import pandas as pd
    import numpy as np
    import tkinter as tk
    root = tk.Tk()
     # create a python dictionary
    dictionary = {
        'Name': ['Microsoft', 'Google', 'Tesla',\
                'Apple.', 'Netflix'],
        'Abre': ['MSFT', 'GOOG', 'TSLA', 'AAPL', 'NFLX'],
        'Industry': ['Technology', 'Technology', 'Automotive', 'Technology',   'Entertainment'],
        'Shares': [50, 500, 150, 200, 80]
    # create dataframe
    df = pd.DataFrame(dictionary)
    # print dataframe
    # reading the data from your clipboard
    data = pd.read_clipboard()
    # show data in GUI
    label = tk.Label(root,text=data)


    • Line 2: We import the pandas package as pd. This alias will be used to work with the DataFrame. 
    • Line 3–4: We import NumPy and Tkinter into the program.
    • Line 5: root = tk.Tk() creates a graphical user interface (GUI) for the root window.
    • Line 7–13: We create a data dictionary with some values. Here we use some industry names and their shares. 
    • Line 15: We create a DataFrame named df.
    • Line 17: We display the DataFrame by invoking print(df).
    • Line 18: We call df.to_clipboard() to copy the entire DataFrame to the clipboard.
    • Line 19: We copy the data to the clipboard while separating them with a comma. By default, the data is separated by a tab ('\t').
    • Line 21: pd.read_clipboard() reads data from the clipboard.
    • Line 23–25: These lines copy data to the GUI of the output tab and keep showing on the console.



    Take a Free Course

    Great engineers are always investing in themselves. Upskill yourself today.

    Keep Exploring
    Related Courses