In questions and answers, users very often post an example DataFrame which their question/answer works with:
In []: x
Out[]:
bar foo
0 4
pd.read_clipboard() is nifty. However, if you're writing code in a script or a notebook (and you want your code to work in the future) it's not a great fit. Here's an alternative way to copy/paste the output of a dataframe into a new dataframe object that ensures that df will outlive the contents of your clipboard:
# py3 only, see below for py2
import pandas as pd
from io import StringIO
d = '''0 1 2 3 4
A Y N N Y
B N Y N N
C N N N N
D Y Y N Y
E N Y Y Y
F Y Y N Y
G Y N N Y'''
df = pd.read_csv(StringIO(d), sep='\s+')
A few notes:
StringIO wraps the output in a file-like object, which read_csv requires.sep to \s+ makes it so that each contiguous block of whitespace is treated as a single delimiter.The above answer is Python 3 only. If you're stuck in Python 2, replace the import line:
from io import StringIO
with instead:
from StringIO import StringIO
If you have an old version of pandas (v0.24 or older) there's an easy way to write a Py2/Py3 compatible version of the above code:
import pandas as pd
d = ...
df = pd.read_csv(pd.compat.StringIO(d), sep='\s+')
The newest versions of pandas have dropped the compat module along with Python 2 support.