Convert text table to pandas dataframe

两盒软妹~` 提交于 2019-12-11 02:38:00

问题


Many times when I'm trying to answer questions on Stackoverflow, the question contains a table, which I have to convert to a pandas dataframe in order to process. for example, in this question:

http://stackoverflow.com/questions/43172116/pandas-count-some-value-in-all-columns

My question is, is there a faster way to convert it to a dataframe rather than writing:

df=pd.DataFrame({'graph':[1,2,3,4,5,6],
0:['blue','blue','red','red','blue','blue'],
1:['blue','blue','red','blue','red','blue'],
2:['blue','blue','blue','red','blue','blue'],
3:['blue','blue','blue','red','red','blue'],
4:['blue','blue','red','blue','red','blue']})

given that I can copy the text:

graph   0       1       2       3       4
1       blue    blue    blue    blue    blue
2       blue    blue    blue    blue    blue
3       blue    red     blue    blue    red
4       red     blue    red     red     blue
5       red     red     blue    red     red
6       blue    blue    blue    blue    blue

回答1:


Make sure the desired data set is in clipboard and use pd.read_clipboard() method.

Step by step:

  1. mark desired data set
  2. press Ctrl+C (for MS Windows)
  3. execute: df = pd.read_clipboard()

In [40]: df = pd.read_clipboard()

In [41]: df
Out[41]:
   graph     0     1     2     3     4
0      1  blue  blue  blue  blue  blue
1      2  blue  blue  blue  blue  blue
2      3  blue   red  blue  blue   red
3      4   red  blue   red   red  blue
4      5   red   red  blue   red   red
5      6  blue  blue  blue  blue  blue


来源:https://stackoverflow.com/questions/43172452/convert-text-table-to-pandas-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!