How to copy/paste DataFrame from Stack Overflow into Python

前端 未结 3 683
耶瑟儿~
耶瑟儿~ 2020-11-27 13:28

In questions and answers, users very often post an example DataFrame which their question/answer works with:

In []: x
Out[]: 
   bar  foo
0    4         


        
3条回答
  •  生来不讨喜
    2020-11-27 14:10

    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:

    • The triple-quoted string preserves the newlines in the output.
    • StringIO wraps the output in a file-like object, which read_csv requires.
    • Setting sep to \s+ makes it so that each contiguous block of whitespace is treated as a single delimiter.

    update

    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.

提交回复
热议问题