Skip to Content

Copy Pandas DataFrame to the Clipboard

In this tutorial, we’ll look at how to copy the contents of a pandas dataframe to your computer’s clipboard.

You can use the pandas dataframe to_clipboard() function to copy the contents of a dataframe to your computer’s clipboard. The following is the syntax:

df.to_clipboard()

Here, df is the dataframe you want to copy. With the dataframe copied to the clipboard, you can paste it where you want. For example, spreadsheet software like Excel or a text file in notepad.

Let’s look at how to use the above function through some examples. First, we’ll create a sample dataframe that we’ll be using throughout this tutorial.

import pandas as pd

data = {
    'Name': ['Microsoft Corporation', 'Google, LLC', 'Tesla, Inc.',\
             'Apple Inc.', 'Netflix, Inc.'],
    'Symbol': ['MSFT', 'GOOG', 'TSLA', 'AAPL', 'NFLX'],
    'Industry': ['Tech', 'Tech', 'Automotive', 'Tech', 'Entertainment'],
    'Shares': [100, 50, 150, 200, 80]
}

# create dataframe
df = pd.DataFrame(data)
# print dataframe
print(df)

Output:

                    Name Symbol       Industry  Shares
0  Microsoft Corporation   MSFT           Tech     100
1            Google, LLC   GOOG           Tech      50
2            Tesla, Inc.   TSLA     Automotive     150
3             Apple Inc.   AAPL           Tech     200
4          Netflix, Inc.   NFLX  Entertainment      80

Here, we’ve created a dataframe representing a sample stock portfolio with information on the name, symbol, industry, and the respective shares invested.

Let’s copy the entire dataframe created above to the clipboard using the to_clipboard() function.

df.to_clipboard()

Now that we have copied the dataframe to the clipboard, we can go ahead and paste it into an excel file. The image below shows how the excel file looks after pasting the data from the clipboard.

The excel file after pasting the dataframe from the clipboard

Notice that the index is also copied here. You can omit the index if you pass index=False to the to_clipboard() function. Let’s see it in action for the above dataframe:

df.to_clipboard(index=False)

The above code copies the dataframe without its index to the clipboard. We can go ahead and paste it into an excel file. The image below shows how the excel file looks after pasting the data from the clipboard.

Excel file after pasting dataframe without index from the clipboard.

There’s no column for the dataframe’s index since we specified not to copy it to the clipboard by passing False to the index parameter of the to_clipboard() function.

Since the dataframe contents are copied to the clipboard, you can paste them into any file you want. For example, if you paste the contents of the above dataframe with index copied to the clipboard to a text file in notepad, this is how it’ll look.

Dataframe values copied to notepad

Notice that each column is separated by tab space. This is because, by default, the pandas to_clipboard() function uses a tab space '\t' character as the delimiter.

You can specify custom delimiters as well. For example, to use comma, pass ',' to the sep parameter of the function.

df.to_clipboard(sep=',')

If you now paste the clipboard contents to notepad, this is how it’ll look.

Dataframe contents pasted from clipboard to notepad with comma as delimiter.

For more on the pandas to_clipboard() function, refer to its documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.