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
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.
Here are the parameters for the
read_clipboard() function as stated in the official documentation:
|A string or regex delimiter. The default of |
The return value of the
read_clipboard() function is a DataFrame, i.e., a parsed DataFrame object.
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:
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
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:
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'])
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
Here are the parameters for the
to_clipboard() function as stated in the official documentation:
|Produce output in a CSV format for easy pasting into excel.|
|These parameters will be passed to |
We will use the data frame we created in the first section for this purpose:
Now, we apply the
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:
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:
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 “
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.
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.