10.6 Clipboard

A handy way to grab data is to use the read_clipboard method, which takes the contents of the clipboard buffer and passes them to the read_table method. For instance, you can copy the following text to the clipboard (CTRL-C on many operating systems):

  A B C
x 1 4 p
y 2 5 q
z 3 6 r

And then import the data directly to a DataFrame by calling:

clipdf = pd.read_clipboard()
In [1]: clipdf
Out[1]: 
   A  B  C
x  1  4  p
y  2  5  q
z  3  6  r

The to_clipboard method can be used to write the contents of a DataFrame to the clipboard. Following which you can paste the clipboard contents into other applications (CTRL-V on many operating systems). Here we illustrate writing a DataFrame into clipboard and reading it back.

In [2]: df = pd.DataFrame(randn(5,3))

In [3]: df
Out[3]: 
        0       1       2
0  0.4691 -0.2829 -1.5091
1 -1.1356  1.2121 -0.1732
2  0.1192 -1.0442 -0.8618
3 -2.1046 -0.4949  1.0718
4  0.7216 -0.7068 -1.0396

In [4]: df.to_clipboard()

In [5]: pd.read_clipboard()
Out[5]: 
        0       1       2
0  0.4691 -0.2829 -1.5091
1 -1.1356  1.2121 -0.1732
2  0.1192 -1.0442 -0.8618
3 -2.1046 -0.4949  1.0718
4  0.7216 -0.7068 -1.0396

We can see that we got the same content back, which we had earlier written to the clipboard.

Note

You may need to install xclip or xsel (with gtk or PyQt4 modules) on Linux to use these methods.