Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed data in an IPython Notebook?

Seems to me that there ought to be a way to read data from a file, ideally into a Pandas DataFrame and create the result in such a way that it becomes part of the notebook, so for instance you can store the data right in the notebook without needing external files?

That way you can send entire examples (obviously mainly for smaller data sets). It would also make doing examples way easier here on SO..

Any Ideas? Even via cut and paste ie output of a dataframe display?

like image 860
dartdog Avatar asked Feb 03 '14 22:02

dartdog


2 Answers

You could put this in an IPython cell:

import pandas as pd
import io
content = '''\
<<PASTE DATA HERE>>
'''

df = pd.read_table(io.BytesIO(content), ...)
like image 171
unutbu Avatar answered Sep 22 '22 14:09

unutbu


To elaborate for others: The dataframe data as below, was simply cut and pasted from the HTML rpr in IPython Notebook

import pandas as pd
import io
content2 = '''\
Units   lastqu  Uperchg lqperchg    fcast   errpercent  nfcast  fctperchg
2000-12-31   19391   NaN     NaN     NaN     NaN     NaN     NaN    NaN
2001-12-31   35068   5925    80.85   NaN     32838   -6.79   NaN    NaN
2002-12-31   39279   8063    12.01   36.08   39750   1.18    42449  NaN
2003-12-31   47517   9473    20.97   17.49   44309   -7.24   43784  NaN
2004-12-31   51439   11226   8.25    18.51   49976   -2.93   53594  NaN
2005-12-31   59674   11667   16.01   3.93    51402  -16.09   52907  NaN
2006-12-31   58664   14016   -1.69   20.13   58997   0.56    68491  NaN
2007-12-31   55698   13186   -5.06   -5.92   56313   1.09    55995  NaN
2008-12-31   42235   11343  -24.17  -13.98   50355   16.13   49805  NaN
2009-12-31   40478   7867    -4.16  -30.64   39117   -3.48   32809  NaN
2010-12-31   38722   8114    -4.34   3.14    39915   2.99    41304  NaN
2011-12-31   36965   8361    -4.54   3.04    40714   9.21    39497  NaN
2012-12-31   39132   8608    5.86    2.95    41512   5.73    37690  NaN
2013-12-31   43160   9016    10.29   4.74    42832   -0.77   40376  NaN
2014-12-31   NaN     9785    NaN     8.53    45318   NaN     45665   5
'''
df2 = pd.read_table(io.BytesIO(content2))
df2

Results in a totally useable DataFrame

like image 33
dartdog Avatar answered Sep 22 '22 14:09

dartdog