In this tutorial, we’ll look at how to copy the contents of a pandas dataframe to your computer’s clipboard.
How to copy a dataframe to the 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.
Examples
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.
Copy Dataframe values to an Excel file
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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

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.

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.
Copy DataFrame values to a Text file
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.

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.

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.