pandas: Get clipboard contents as DataFrame with read_clipboard()

Posted: | Tags: Python, pandas

You can use pandas.read_clipboard() to read the clipboard contents as a DataFrame. It is very useful when used with IPython or Jupyter Notebook.

to_clipboard() is also provided to copy the contents of a DataFrame to the clipboard. See the following article.

You can also work with the clipboard with pyperclip.

read_clipboard()

By default, the sep parameter is set to '\s+', and whitespace characters are treated as a delimiter.

When you copy a table from a web page or cells from spreadsheet software such as Excel or Numbers, it is copied tab-separated and can be converted directly into a DataFrame by read_clipboard().

Here is an example of copying a table from Python on Japanese Wikipedia (as of January 2018) and running read_clipboard(). The DataFrame is saved as a CSV file using the to_csv() method.

import pandas as pd

df = pd.read_clipboard()
print(df)
#    バージョン    リリース日[16]
# 0    3.0   2008年12月3日
# 1    3.1   2009年6月27日
# 2    3.2   2011年2月20日
# 3    3.3   2012年9月29日
# 4    3.4   2014年3月16日
# 5    3.5   2015年9月13日
# 6    3.6  2016年12月23日

df.to_csv('data/dst/test.csv')

If the copied text is not separated by whitespace characters, set the sep parameter accordingly. For example, if the content is comma , delimited, set read_clipboard(sep=',').

Since read_clipboard() internally passes the clipboard contents to read_csv(), you can specify the same parameters as for read_csv().

For example, by default, the first row (header) is treated as the column names columns as in the example above. For a table with no header, just set header=None.

Note that it is easier to use read_html() or read_excel() rather than copying to the clipboard when periodically reading a table from a specific web page or reading data at a specific location from many Excel files with the same layout.

Related Categories

Related Articles