Read and Write Clipboard with Pandas

In this tutorial, we will learn how to read copied tabular data from our clipboard using the read_clipboard() function and how to copy a data frame to our clipboard to paste it somewhere by applying the to_clipboard() function.

Read Data from the Clipboard

When we come across an interesting dataset, for example on the web, that we would like to use for a data science project, we could access it by downloading the dataset and saving it locally on our computer. This approach works but it is often a bit inconvenient, and the dataset demands disk space. Most of these files are small, however, if we do that frequently, we find our memory overflowed by various datasets.

Luckily, Pandas provides us with the useful read_clipboard() function that reads data from our clipboard. In other words, when we find an interesting dataset, we can just mark it and copy it by right-clicking on it and select β€œcopy” or by using CTRL/CMD + C on Windows and macOS, respectively.

Syntax

Here are the parameters for the read_clipboard() function as stated in the official documentation:

Parameters:

NameData TypeDescription
sepstr, default 's+'A string or regex delimiter. The default of 's+' denotes one or more whitespace characters
**kwargsNoneSee read_csv for the full argument list

Returns Value

The return value of the read_clipboard() function is a DataFrame, i.e., a parsed DataFrame object.

Example

Let’s have a look at a sample dataset. This dataset can be from anywhere: from the web, from a file, etc. For our example, we will use a dataset from an excel file.

It does not matter where this file is stored since we do not import the file. We just copy the data.

We mark the data and copy it to our clipboard.

Now, we head over to our code editor:

import pandas as pd
pd.read_clipboard()

The resulting DataFrame:

NameAgePetsChildren
0Jane2912
1Bob3221
2Alice4103

First, we import the Pandas library. Then we apply the read_clipboard() function. The output shows a Pandas data frame. That means, we read the data successfully.

To be able to work with the copied data, we assign the data to a variable:

df = pd.read_clipboard()

Apply parameters from the read_csv() function

The read_clipboard() function works this way: It reads the data from the clipboard and then passes the data to the read_csv() function. That means we can use the parameters that the read_csv() function supplies us with. You can read more about the read_csv() function in the official documentation.

Let’s say, we have a dataset like this:

This is essentially the same data as before, but we do not have the headers and no indexes here.

If we copy this data and apply the read_clipboard() function, this is what we get:

pd.read_clipboard()
Jane2912
0Bob3221
1Alice4103

The indexes are created by default, but the first row of the dataset is now the header of the data frame which is not what we intended. Fortunately, we can use the parameters from the read_csv() function. In this case, we apply the β€œnames” parameter which expects a list of the column names:

pd.read_clipboard(names=['Name', 'Age', 'Pets', 'Children'])
NameAgePetsChildren
0Jane2912
1Bob3221
2Alice4103

We fill the β€œnames” list with the column names that we want to get. We use the same column names as in the initial dataset. This way, we get a data frame with reasonable headers.

Copy a data frame to our clipboard

We learned how to read data from our clipboard and how to transform it into a data frame. In this section, we will get to know the other way around: copy a data frame to the system clipboard using the to_clipboard() function.

Here are the parameters for the to_clipboard() function as stated in the official documentation:

NameData TypeDescription
excelbool, default TrueProduce output in a CSV format for easy pasting into excel.
True, use the provided separator for CSV pasting.
False, write a string representation of the object to the clipboard.
sepstr, default '\t'Field delimiter.
**kwargsNoneThese parameters will be passed to DataFrame.to_csv.

We will use the data frame we created in the first section for this purpose:

print(df)
NameAgePetsChildren
0Jane2912
1Bob3221
2Alice4103

Now, we apply the to_clipboard() function:

df.to_clipboard()

This does not produce an output, but the data frame is now copied to our clipboard, so we can paste it somewhere.

For example, we can paste it into an excel file:

Or into a simple text editor:

When we have a look at the documentation, we see that the β€œexcel” parameter is set to β€œTrue” by default. This way, we produce a data frame in the CSV format. That makes it easy to paste into excel. If we set that to β€œFalse”, we copy a string representation of the data frame to our clipboard:

df.to_clipboard(excel=False)

And this is what it looks like when we paste that into excel:

As we can see, it is not suitable this way for an excel file.

Additionally, we can change the default separator by applying the β€œsep” parameter:

df.to_clipboard(sep=',')

If we paste that into an excel file, it looks like this:

We often find data with different separators, so it’s nice to be able to change the separator easily.

And if we want to get rid of the indexes, we can set the β€œindex” parameter to β€œFalse”:

df.to_clipboard(index=False)

Which looks like this in an excel file:

So, as we can see, there are numerous ways to copy data to the clipboard. Which way we choose, depends on where we want to paste the data.

Summary

All in all, we learned how to use our clipboard for working with Pandas. We saw how to read data to our clipboard to make it available in Pandas and how to copy Pandas data frames in various ways to our clipboard to paste it anywhere. These skills are very useful because it saves us a lot of time when working with lots of different datasets.

For more tutorials about Pandas, Python libraries, Python in general, or other computer science-related topics, check out the Finxter Blog page.

Happy Coding!