Given this data frame from some other question:
Constraint Name TotalSP Onpeak Offpeak
Constraint_ID
77127 aaaaaaaaaaaaaaaa
This is not as cool as @ayhan's answer, but most of the time works pretty well. Assuming you are using ipython or jupyter, just copy and paste the data into %%file
:
Then do some quick edits. With multi-indexes, just move the index up a line, something like this (also shortening "Constraint ID" to "ID" to save a little space in this case):
%%file foo.txt
ID Constraint Name TotalSP Onpeak Offpeak
77127 aaaaaaaaaaaaaaaaaa -2174.5 -2027.21 -147.29
98333 bbbbbbbbbbbbbbbbbb -1180.62 -1180.62 0
1049 cccccccccccccccccc -1036.53 -886.77 -149.76
pd.read_fwf('foo.txt')
Out[338]:
ID Constraint Name TotalSP Onpeak Offpeak
0 77127 aaaaaaaaaaaaaaaaaa -2174.50 -2027.21 -147.29
1 98333 bbbbbbbbbbbbbbbbbb -1180.62 -1180.62 0.00
2 1049 cccccccccccccccccc -1036.53 -886.77 -149.76
read_fwf
generally works pretty well on tabular stuff like this, correctly dealing with spaces in column names (usually). Of course, you can also use this basic method with read_csv
.
The nice thing about this method is that for small sample data you can deal with just about any of the weird ways that users post data here. And there are a lot of weird ways. ;-)