遍历Jinja2中的 pandas 数据框 [英] Iterate over pandas dataframe in jinja2

查看:72
本文介绍了遍历Jinja2中的 pandas 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数据框:

        id       text
 0      12       boats
 1      14       bicycle
 2      15       car

现在,我要在jinja2中进行选择.但是我找不到在jinja2中循环遍历数据框的方法.

Now I want to make a select dropdown in jinja2. But I cannot find a way to loop over the dataframe in jinja2.

我尝试使用to_dict().但是

I tried using to_dict(). But with

{% for key,value in x.items() %}

它遍历id和文本而不是行.如何更改此设置,以便可以在模板中执行类似的操作?

it loops over id and text instead of the rows. How can I change this so I can do something like this in the template?

{% for key,value in x.items() %}
    <option value="{{ id }}">{{ text }}</option>
{% endfor %}

推荐答案

正如John Galt所建议的那样:

As John Galt suggested this works:

{% for key,value in x.iterrows() %}
      <option value="{{ value['id'] }}">{{ value['text'] }}</option>
{% endfor %}

这篇关于遍历Jinja2中的 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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