pandas 从 iPython Notebook 下载 CSV

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31893930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:41:00  来源:igfitidea点击:

Download CSV from an iPython Notebook

csvpandasipython-notebook

提问by Tom Slee

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 Notebook 服务器,并希望用户能够将 Pandas 数据帧下载为 csv 文件,以便他们可以在自己的环境中使用它。没有个人数据,所以如果解决方案涉及在服务器上写入文件(我可以这样做)然后下载该文件,我会很高兴。

回答by Coen Jonker

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:

如何使用 IPython 的 FileLinks 类?我使用它来提供直接从 Jupyter 笔记本访问数据的权限。假设您的数据在 Pandas 数据帧 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/')

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.

将此作为笔记本单元运行,结果将是可直接从笔记本下载的文件链接列表。'/path/to'当然,笔记本用户需要可以访问。

回答by Yasin Z?hringer

For not too large tables you can use the following code:

对于不太大的表,您可以使用以下代码:

import base64
import pandas as pd
from IPython.display import HTML

def create_download_link( df, title = "Download CSV file", filename = "data.csv"):
    csv = df.to_csv()
    b64 = base64.b64encode(csv.encode())
    payload = b64.decode()
    html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>'
    html = html.format(payload=payload,title=title,filename=filename)
    return HTML(html)

df = pd.DataFrame(data = [[1,2],[3,4]], columns=['Col 1', 'Col 2'])
create_download_link(df)

回答by Kim C.

If you want to avoid storing CSVs on the server, you can use this Javascript alternative that create the CSV on the client-side:

如果您想避免在服务器上存储 CSV,您可以使用此 Javascript 替代方法在客户端创建 CSV:

from IPython.display import Javascript
js_download = """
var csv = '%s';

var filename = 'results.csv';
var blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
if (navigator.msSaveBlob) { // IE 10+
    navigator.msSaveBlob(blob, filename);
} else {
    var link = document.createElement("a");
    if (link.download !== undefined) { // feature detection
        // Browsers that support HTML5 download attribute
        var url = URL.createObjectURL(blob);
        link.setAttribute("href", url);
        link.setAttribute("download", filename);
        link.style.visibility = 'hidden';
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    }
}
""" % data_in_dataframes.to_csv(index=False).replace('\n','\n').replace("'","\'")

Javascript(js_download)

Basically, it creates a CSV string in python from the pd dataframe and use it in a small js script that creates a CSV file on the client sideand open a saving dialog to save it on the user computer. I tested in my iPython env and it works like a charm!

基本上,它从 pd 数据帧在 python 中创建一个 CSV 字符串,并在一个小的 js 脚本中使用它,该脚本在客户端创建一个 CSV 文件并打开一个保存对话框以将其保存在用户计算机上。我在我的 iPython 环境中进行了测试,它就像一个魅力!



Note that I am escaping the \n. If I don't do so, the js script string will have the CSV variable written on multiple lines.

请注意,我正在逃避\n. 如果我不这样做,js 脚本字符串将在多行上写入 CSV 变量。

For example, print "var csv = '%s'" % industries_revenues.to_csv(index=False).replace('\n','\\n')results to this:

例如,print "var csv = '%s'" % industries_revenues.to_csv(index=False).replace('\n','\\n')结果如下:

var csv = 'Industry,sum_Amount\nBanking,65892584.0\n(...)Finance,20211917.0\n'

Instead of print "var csv = '%s'" % industries_revenues.to_csv(index=False)without the \nescaping that results on a multiple lined and therefore errored javascript:

而不是print "var csv = '%s'" % industries_revenues.to_csv(index=False)没有\n转义导致多行并因此错误的javascript:

var csv = 'Industry,sum_Amount
Banking,65892584.0
(...)
Finance,20211917.0
'

I also escape the 'not to break the variable string in javascript.

我也逃避了'不破坏javascript中的变量字符串。

回答by frankjania

You can use the fact that the notebook can display html for objects, and data urls, to make the content of a csv downloadable:

您可以使用笔记本可以显示对象的 html 和数据 url 的事实,使 csv 的内容可下载:

import urllib

class CSV(object):
    def _repr_html_(self):
        html = []

        html.append("{},{},{}".format(
                "user",
                "age",
                "city"
            )
        )

        html.append("{},{},{}".format(
                "Alice",
                "39",
                "New York"
            )
        )

        html.append("{},{},{}".format(
                "Bob",
                "30",
                "Denver"
            )
        )

        html.append("{},{},{}".format(
                "Carol",
                "27",
                "Tulsa"
            )
        )


        export = '\n'.join(html)
        export = urllib.quote(export.encode("utf-8"))
        csvData = 'data:application/csv;charset=utf-8,' + export
        return "<a download='export.csv' href='{}' target='_blank'>csv file</a>".format(csvData)

CSV()

回答by Paul Rougieux

A function that creates a csv download link, based on Coen Jonker's answer.

基于 Coen Jonker 的回答创建 csv 下载链接的函数。

The function has an optional delete prompt so you can delete the file after download to keep the notebook server clean.

该功能有一个可选的删除提示,因此您可以在下载后删除文件以保持笔记本服务器的清洁。

def csv_download_link(df, csv_file_name, delete_prompt=True):
    """Display a download link to load a data frame as csv from within a Jupyter notebook"""
    df.to_csv(csv_file_name, index=False)
    from IPython.display import FileLink, FileLinks
    display(FileLink(csv_file_name))
    if delete_prompt:
        a = input('Press enter to delete the file after you have downloaded it.')
        import os
        os.remove(csv_file_name)
# Diplay download link:
csv_download_link(df, 'file_name.csv')

回答by Ash Upadhyay

My simple approach to download all the files from the jupyter notebook would be by simply using this wonderful command

我从 jupyter notebook 下载所有文件的简单方法就是使用这个美妙的命令

!tar cvfz my_compressed_file_name.tar.gz *

!tar cvfz my_compressed_file_name.tar.gz *

This will download all the files of the server including the notebooks.

这将下载服务器的所有文件,包括笔记本。

In case if your server has multiple folders, you might be willing to use the following command. write ../ before the * for every step up the directory.

如果您的服务器有多个文件夹,您可能愿意使用以下命令。在目录的每一步的 * 之前写 ../ 。

tar cvfz zipname.tar.gz ../../*

tar cvfz zipname.tar.gz ../../*

Hope it helps..

希望能帮助到你..