How to copy/paste DataFrame from Stack Overflow into Python

前端 未结 3 681
耶瑟儿~
耶瑟儿~ 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:07

    If you are copy-pasting from CSV file which has standard entries like this:

    2016,10,M,0600,0610,13,1020,24
    2016,3,F,0300,0330,21,6312,1
    2015,4,M,0800,0830,8,7112,30
    2015,10,M,0800,0810,19,0125,1
    2016,8,M,1500,1510,21,0910,2
    2015,10,F,0800,0810,3,8413,5
    
    df =pd.read_clipboard(sep=",", header=None)
    df.rename(columns={0: "Name0", 1: "Name1",2:"Name2",3:"Name3",4:"Name4",5:"Name5",6:"Name6",7:"Name7",8:"Name8"})
    

    will give you properly defined pandas Dataframe.

提交回复
热议问题