Create Pandas DataFrame from a string

后端 未结 5 1583
灰色年华
灰色年华 2020-11-22 09:01

In order to test some functionality I would like to create a DataFrame from a string. Let\'s say my test data looks like:

TESTDATA=\"\"\"col1;co         


        
5条回答
  •  遇见更好的自我
    2020-11-22 09:38

    A quick and easy solution for interactive work is to copy-and-paste the text by loading the data from the clipboard.

    Select the content of the string with your mouse:

    In the Python shell use read_clipboard()

    >>> pd.read_clipboard()
      col1;col2;col3
    0       1;4.4;99
    1      2;4.5;200
    2       3;4.7;65
    3      4;3.2;140
    

    Use the appropriate separator:

    >>> pd.read_clipboard(sep=';')
       col1  col2  col3
    0     1   4.4    99
    1     2   4.5   200
    2     3   4.7    65
    3     4   3.2   140
    
    >>> df = pd.read_clipboard(sep=';') # save to dataframe
    

提交回复
热议问题