(Note that if using this approach for pasting dataframes into StackOverflow, use CSV and not TSV, see here.)
Without need for a separate file; although list could could be in separate file, and then just not use StringIO. Handy for toy code and keeping examples compact and as one file.
IMPORTANT THING IS TO HAVE import SOURCE MATCH VERSION OF PYTHON, check with import sys;print (sys.version)
. Or use provided approach compatible with both Python 2 and 3.
You can get this entire thing as Python code in the raw gist.
This markdown renders nicely here.
import pandas as pd
import numpy as np
#"For examples that use the StringIO class, make sure you import it according to your
# Python version, i.e. `from StringIO import StringIO` for Python 2 and
# `from io import StringIO for Python 3`." - http://pandas.pydata.org/pandas-docs/stable/io.html
# PICK CORRECT IMPORT:
#from StringIO import StringIO # see http://pandas.pydata.org/pandas-docs/stable/io.html
#from io import StringIO # see http://pandas.pydata.org/pandas-docs/stable/io.html
#-OR-
# Use approach that works in both if trying to make work cross-versions
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
input ='''
Id Speed
1 30
1 35
1 31
2 20
2 25
3 80
'''
# conversion from original post before I recalled how to do from docstring with StringIO
'''
df = pd.DataFrame({ 'Id' : [1,1,1,2,2,3],
'Speed':[30,35,31,20,25,80]
})
'''
df = pd.read_table(StringIO(input), header=0, index_col=None, delim_whitespace=True)
print(df)