5

I notice a lot of Pandas questions on Stack Overflow only include a few rows of their data as text, without the accompanying code to generate/reproduce it. I am aware of the existence of read_clipboard, but I am unable to figure out how to effectively call this function to read data in many situations, such as when there are white spaces in the header names, or Python objects such as lists in the columns.

How can I use pd.read_clipboard more effectively to read data pasted in unconventional formats that don't lend themselves to easy reading using the default arguments? Are there situations where read_clipboard comes up short?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cs95
  • 379,657
  • 97
  • 704
  • 746
  • Made sure main was the right place to post this by asking [a question on meta](https://meta.stackoverflow.com/questions/403870/should-my-canonical-about-read-clipboard-usage-in-pandas-be-on-meta-or-main/403875?noredirect=1#comment815449_403875). – cs95 Dec 20 '20 at 10:47

2 Answers2

9

read_clipboard: Beginner's Guide


read_clipboard is truly a saving grace for anyone starting out to answer questions in the Pandas tag. Unfortunately, pandas veterans also know that the data provided in questions isn't always easy to grok into a terminal due to various complications in the format of the data posted.

Thankfully, read_clipboard has arguments that make handling most of these cases possible (and easy). Here are some common use cases and their corresponding arguments.


Common Use Cases

read_clipboard uses read_csv under the hood with white space separator, so a lot of the techniques for parsing data from CSV apply here, such as

  • parsing columns with spaces in the data

    • use sep with regex argument. First, ensure there are at least two spaces between columns and at most one consecutive white space inside the column's data itself. Then you can use sep=r'\s{2,}' which means "separate columns by looking for at least two consecutive white spaces for the separator" (note: engine='python' is required for multicharacter or regex separators):

       df = pd.read_clipboard(..., sep=r'\s{2,}', engine='python')
      

      Also see How do you handle column names having spaces in them when using pd.read_clipboard?.

  • reading a series instead of DataFrame

  • loading data with custom header names

    • use names=[...] in conjunction with header=None and skiprows=[0] to ignore existing headers.

       df = pd.read_clipboard(..., header=None, names=['a', 'b', 'c'], skiprows=[0])
      
  • loading data without any headers

    • use header=None
  • set one or more columns as the index

    • use index_col=[...] with the appropriate label or index
  • parsing dates

    • use parse_dates with the appropriate format. If parsing datetimes (i.e., columns with date separated by timestamp), you will likely also need to use sep=r'\s{2,}' while ensuring your columns are separated by at least two spaces.

See this answer by me for a more comprehensive list on read_csv arguments for other cases not covered here.


Caveats

read_clipboard is a Swiss Army knife. However, it

Zoe
  • 27,060
  • 21
  • 118
  • 148
cs95
  • 379,657
  • 97
  • 704
  • 746
2

The one weakness of this function is that it doesn't capture contents of Ctrl + C if the copy is performed from a PDF file. Testing it this way results in an empty read.

But by using a regular text editor, it goes just fine. Here is an example using randomly typed text:

>>> pd.read_clipboard()
Empty DataFrame
Columns: [sfsesfsdsxcvfsdf]
Index: []
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
etch_45
  • 792
  • 1
  • 6
  • 21
  • 1
    Hopefully no one is going around pasting pdfs of their data for folks to use while answering questions here, but this is a general concern. Well, this, and being unable to parse text from images, which are all too commonly posted on this site. :-( If you're up to make a tesseract extension that does this, you'll be doing a pretty big service. – cs95 Dec 20 '20 at 11:24
  • That's something to look into. I have never worked with tesseract but always up to learn something new. Thanks @cs95! – etch_45 Dec 20 '20 at 11:27