从 iPython Notebook 下载 CSV [英] Download CSV from an iPython Notebook

查看:36
本文介绍了从 iPython Notebook 下载 CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行 iPython Notebook 服务器,并希望用户能够将 Pandas 数据帧下载为 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 Notebook 下载 CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆