How can I convert a table that have being copy-pasted to a pandas DataFrame?

559 Views Asked by At

How can I convert a table that have being copy-pasted to a pandas DataFrame?

As an example, I have the following table that have being copy-pasted:

Step    Training Loss   Validation Loss
5000    8.524000    6.308433
10000   6.043500    5.703260
15000   5.556500    5.274880
20000   5.203900    4.956189
25000   4.925100    4.714767
30000   4.708400    4.511205
35000   4.541200    4.338839
40000   4.380300    4.211163
45000   4.262100    4.087589
50000   4.154100    3.984569
55000   4.060400    3.890067
60000   3.965400    3.807854
65000   3.894100    3.728049
70000   3.814200    3.654750
75000   3.754300    3.597961
80000   3.695300    3.534978
85000   3.644300    3.490259
90000   3.595700    3.439605
95000   3.545500    3.385926
100000  3.496100    3.351504
105000  3.461700    3.298338
110000  3.418300    3.278054
2

There are 2 best solutions below

0
On

Some suggested to use pd.read_clipboard(sep=sep=r'\s{2,}', engine='python') which is nice. But it didn't work for me because I was working on a Virtual Machine at Google Cloud Platform. So I've copy-pasted the table into a text editor, saved as csv and opened it using read_csv("file.csv", sep="\t").

0
On

You could also use StringIO to simulate the csv-file:

import pandas as pd
from io import StringIO

data_string = r'''Step  Training Loss   Validation Loss
5000    8.524000    6.308433
10000   6.043500    5.703260
15000   5.556500    5.274880
20000   5.203900    4.956189'''

data_io = StringIO(data_string)
df = pd.read_csv(data_io, delimiter='\t')