Convert text table to pandas dataframe

Many times when I try to answer questions in Stackoverflow, the question contains a table that I have to convert to a pandas framework for processing. 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 
+2
source share
1 answer

Make sure the data set you need is on the clipboard and use the pd.read_clipboard () method .

Step by step:

  • mark the desired data set
  • press Ctrl + C (for MS Windows)
  • 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 
+5
source

Source: https://habr.com/ru/post/1267068/


All Articles