从iPython笔记本下载CSV [英] Download CSV from an iPython Notebook
问题描述
我运行的是iPython Notebook服务器,希望用户能够将csv文件下载为熊猫数据框,以便他们可以在自己的环境中使用它.没有个人数据,因此,如果解决方案包括在服务器上写文件(我可以这样做)然后下载该文件,我会很满意的.
I run an iPython Notebook server, and would like users to be able to download a pandas dataframe as a csv file so that they can use it in their own environment. There's no personal data, so if the solution involves writing the file at the server (which I can do) and then downloading that file, I'd be happy with that.
推荐答案
如何使用IPython中的FileLinks类?我使用它来直接从Jupyter笔记本提供对数据的访问.假设您的数据在pandas数据框p_df中:
How about using the FileLinks class from IPython? I use this to provide access to data directly from Jupyter notebooks. Assuming your data is in pandas dataframe p_df:
from IPython.display import FileLink, FileLinks
p_df.to_csv('/path/to/data.csv', index=False)
p_df.to_excel('/path/to/data.xlsx', index=False)
FileLinks('/path/to/')
将其作为笔记本电脑单元运行,结果将是可直接从笔记本电脑下载的文件的链接列表.当然,笔记本用户需要可以访问'/path/to'
.
Run this as a notebook cell and the result will be a list of links to files downloadable directly from the notebook. '/path/to'
needs to be accessible for the notebook user of course.
这篇关于从iPython笔记本下载CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!