Pandas 数据框到烧瓶模板作为 json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48050769/
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-14 04:59:42  来源:igfitidea点击:

Pandas dataframe to flask template as a json

pythonjsonpandasflask

提问by Tobi

I try to output a pandas dataframe in a flask template. My idea was to transform it to json and then loop through the table

我尝试在烧瓶模板中输出Pandas数据框。我的想法是将其转换为 json 然后遍历表

I tested this

我测试了这个

jsonfiles = df.to_json(orient='records')

return render_template('index.html', ctrsuccess=jsonfiles)

In the template I try to output like this:

在模板中,我尝试像这样输出:

        {%if ctrsuccess %}
        <div class="table-responsive">
            <table class="table table-striped">
                <tbody>
                    {%for ctrrow in ctrsuccess %}
                    <tr>
                        <td>{{ ctrrow['positionint'] }}</td>
                    </tr>
                    {%endfor%}
                </tbody>
            </table>
        </div>
        {%endif%}

In the template i get a proper looking json if i just draw ctrsuccess. But the table is empty

在模板中,如果我只是绘制 ctrsuccess,我会得到一个看起来很合适的 json。但是桌子是空的

[{"positionint":1.0,"clicks":1138.0,"impressions":4768.0,"ctr":0.2386744966,"clickCtr10p":1251.8,"clickCtr50p":1707.0,"clickCtr100p":2276.0},{"positionint":2.0,"clicks":3160.0,"impressions":49899.0,"ctr":0.0633279224,"clickCtr10p":3476.0,"clickCtr50p":4740.0,"clickCtr100p":6320.0},{"positionint":3.0,"clicks":1255.0,"impressions":50020.0,"ctr":0.025089964,"clickCtr10p":1380.5,"clickCtr50p":1882.5,"clickCtr100p":2510.0},{"positionint":4.0,"clicks":363.0,"impressions":39077.0,"ctr":0.0092893518,"clickCtr10p":399.3,"clickCtr50p":544.5,"clickCtr100p":726.0},{"positionint":5.0,"clicks":216.0,"impressions":18419.0,"ctr":0.011727021,"clickCtr10p":237.6,"clickCtr50p":324.0,"clickCtr100p":432.0},{"positionint":6.0,"clicks":307.0,"impressions":15447.0,"ctr":0.0198744093,"clickCtr10p":337.7,"clickCtr50p":460.5,"clickCtr100p":614.0},{"positionint":7.0,"clicks":248.0,"impressions":24709.0,"ctr":0.0100368287,"clickCtr10p":272.8,"clickCtr50p":372.0,"clickCtr100p":496.0},{"positionint":8.0,"clicks":236.0,"impressions":26853.0,"ctr":0.0087885897,"clickCtr10p":259.6,"clickCtr50p":354.0,"clickCtr100p":472.0},{"positionint":9.0,"clicks":139.0,"impressions":26577.0,"ctr":0.0052300862,"clickCtr10p":152.9,"clickCtr50p":208.5,"clickCtr100p":278.0},{"positionint":10.0,"clicks":23.0,"impressions":2046.0,"ctr":0.0112414467,"clickCtr10p":25.3,"clickCtr50p":34.5,"clickCtr100p":46.0},{"positionint":null,"clicks":7085.0,"impressions":257815.0,"ctr":null,"clickCtr10p":7793.5,"clickCtr50p":10627.5,"clickCtr100p":14170.0},{"positionint":null,"clicks":null,"impressions":null,"ctr":null,"clickCtr10p":1417.0,"clickCtr50p":7085.0,"clickCtr100p":14170.0}]

回答by Parfait

Per pandas docs, to_jsonrenders a json stringwhich you can confirm with type(jsonfiles). Though the print output looks like a json object, single quotes will wrap at beginning and end.

根据Pandas文档,to_json呈现一个json 字符串,您可以使用type(jsonfiles). 尽管打印输出看起来像一个 json 对象,但单引号将在开始和结束处换行。

To resolve, simply import the built-in jsonmodule (part of Python standard library) and parse the to_jsonstring into a json object with loadsmethod:

要解决,只需导入内置json模块(Python 标准库的一部分)并to_json使用以下loads方法将字符串解析为 json 对象:

import json
...

jsonfiles = json.loads(df.to_json(orient='records'))

return render_template('index.html', ctrsuccess=jsonfiles)