Skip to content

Instantly share code, notes, and snippets.

@fomightez
Last active April 5, 2024 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fomightez/e183bbc819ce7b188e1c268f9edd1388 to your computer and use it in GitHub Desktop.
Save fomightez/e183bbc819ce7b188e1c268f9edd1388 to your computer and use it in GitHub Desktop.
Text lists ===> Python Pandas dataframe

(Note that if using this approach for pasting dataframes into StackOverflow, use CSV and not TSV, see here.)

Small text lists with column headers ==> Pandas dataframes

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)
# small text lists with column headers ==> Pandas dataframes
# WITHOUT SEPARATE FILE (could be in separate file and then just not use StringIO)
# Handy for toy code and keeping examples compact and as one file.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment