Over your career as a Data Scientist, there may be instances where you will work with data to/from the system Clipboard. This article shows you how to manipulate this data.
Preparation
Before any data manipulation can occur, one (1) new library will require installation.
To install this library, navigate to an IDE terminal. At the command prompt ($
), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($
). Your terminal prompt may be different.
$ pip install pandas
Hit the <Enter>
key on the keyboard to start the installation process.
If the installation was successful, a message displays in the terminal indicating the same.
Feel free to view the PyCharm installation guide for the required library.
Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.
import pandas as pd
Read Data from Clipboard
Function Outline
pandas.read_clipboard(sep='\\s+', **kwargs)
Returns a parsed DataFrame object.
This function reads text from the system Clipboard and passes this data to the read_csv()
function. Pass in the appropriate parameter(s), save, and you have a new CSV containing the Clipboard data!
Example 1
Say you are the Data Scientist for Arc Recordings. Your boss has found a list of the ten most promising songwriters for the year on a website. He wants you to copy this data and forward it to him via a CSV file.
The first step is to copy the contents of the rows and columns below by highlighting the text, then press CTL+C
to copy the contents to the system Clipboard.
Fname | Lname | Songs | Managers-Name | Managers-Phone |
Josh | Washer | 23 | Bob Jones | 1-809-333-1212 |
Michael | Hudson | 13 | Steve Smith | 1-821-321-5444 |
Russ | Cooper | 34 | Cary Waters | 1-302-567-5465 |
Anna | Czecka | 18 | Howie Brown | 1-523-777-8790 |
Luke | Ancaster | 27 | James Kirker | 1-555-412-5657 |
Shane | Kirk | 72 | Sarah Waters | 1-312-666-7879 |
Morgan | Freeds | 21 | Giselle Henri | 1-812-553-6262 |
Ernie | Walker | 43 | Tony Hamilton | 1-902-555-1212 |
Once that is done, run the code below.
df = pd.read_clipboard() print(df)
- Line [1] copies the contents of the system Clipboard to the DataFrame (
df
). Note: by default, the field separator character is whitespace. - Line [2] outputs the contents to the terminal.
Output
The output should be the same as shown above.
Example 2
For this example, we have a list of the name and phone numbers of the Managers from above. The field separator is a comma (,
).
The first step is to copy the contents of the rows and columns below by highlighting the text, then press CTL+C
to copy the contents to the system Clipboard.
Mgr-Name, Mgr-Phone Bob Jones,1-809-333-1212 Steve Smith,1-821-321-5444 Cary Waters,1-302-567-5465 Howie Brown,1-523-777-8790 James Kirker,1-555-412-5657 Sarah Waters,1-312-666-7879 Giselle Henri,1-812-553-6262 Tony Hamilton,1-902-555-1212
Once that is done, run the code below. The separator parameter (,) must exist since we are not parsing on whitespace (the default). Therefore, the separator parameter (,) must exist.
df = pd.read_clipboard(',') print(df)
- Line [1] copies the contents of the system Clipboard to the DataFrame (
df
). The separator this time is a comma (,
). Since the comma is not the default, it must exist. - Line [2] outputs the contents to the terminal.
Output
The output will be similar to the following:
Mgr-Name | Mgr-Phone | |
0 | Bob Jones | 1-809-333-1212 |
1 | Steve Smith | 1-821-321-5444 |
2 | Cary Waters | 1-302-567-5465 |
3 | Howie Brown | 1-523-777-8790 |
4 | James Kirker | 1-555-412-5657 |
5 | Sarah Waters | 1-312-666-7879 |
6 | Giselle Henri | 1-812-553-6262 |
7 | Tony Hamilton | 1-902-555-1212 |
A few additional notes about the Clipboard:
- For all available parameters, click here.
- As seen above, whitespace is the default separator character unless otherwise stipulated.
DataFrame to Clipboard
Function Outline
DataFrame.to_clipboard(excel=True, sep=None, **kwargs)
Copies a DataFrame to the Clipboard.
The first step is to copy the contents of the rows and columns below by highlighting the text, then press CTL+C
to copy the contents to the system Clipboard.
Mgr-Name, Mgr-Phone Bob Jones,1-809-333-1212 Steve Smith,1-821-321-5444 Cary Waters,1-302-567-5465 Howie Brown,1-523-777-8790 James Kirker,1-555-412-5657 Sarah Waters,1-312-666-7879 Giselle Henri,1-812-553-6262 Tony Hamilton,1-902-555-1212
Once that is done, run the code below. The separator parameter must exist since we are not parsing on whitespace (default).
df.to_clipboard(sep=',') print(df)
- Line [1] reads the contents of the system Clipboard to the DataFrame (
df
). The separator this time is a comma (,
). Since the comma is not the default, it must exist. - Line [2] outputs the contents to the terminal.
Output:
Mgr-Name | Mgr-Phone | |
0 | Bob Jones | 1-809-333-1212 |
1 | Steve Smith | 1-821-321-5444 |
2 | Cary Waters | 1-302-567-5465 |
3 | Howie Brown | 1-523-777-8790 |
4 | James Kirker | 1-555-412-5657 |
5 | Sarah Waters | 1-312-666-7879 |
6 | Giselle Henri | 1-812-553-6262 |
7 | Tony Hamilton | 1-902-555-1212 |
Save to CSV
The last step is to save the system Clipboard contents from Section 3 above. If you don`t already have this in your system Clipboard, do this before proceeding.
Run the following code to save the contents of the system Clipboard to the managers.csv
file.
df = pd.read_clipboard(',') df.to_clipboard(sep=',') df.to_csv('managers.csv', encoding='utf-8')
- Line [1] reads the contents of the system clipboard to the DataFrame (
df
). The separator this time is a comma (,
). Since the comma is not the default, it must exist. - Line [2] sends the contents of the DataFrame to the system Clipboard.
- Line [3] saves the contents to the
managers.csv
file. Setting theencoding
parameter catches and prevents any UnicodeEncodeError from occurring.
Open the CSV created and view the contents.
Output

This article uses a small amount of data. However, the functions mentioned above are real-time-savers to produce results in a short amount of time.