[英]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? 在我看来,应该有一种方法从文件中读取数据,理想情况下是Pandas DataFrame,并以这样的方式创建结果,使其成为笔记本的一部分,因此,例如,您可以将数据存储在笔记本中不需要外部文件?
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?
即使通过剪切和粘贴即输出数据帧显示?
You could put this in an IPython cell: 你可以把它放在一个IPython单元格中:
import pandas as pd
import io
content = '''\
<<PASTE DATA HERE>>
'''
df = pd.read_table(io.BytesIO(content), ...)
To elaborate for others: The dataframe data as below, was simply cut and pasted from the HTML rpr in IPython Notebook 详细说明其他人:下面的数据框数据只是从IPython Notebook中的HTML rpr中剪切和粘贴
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 结果是一个完全可用的DataFrame
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.