Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over pandas dataframe in jinja2

I have this dataframe:

        id       text
 0      12       boats
 1      14       bicycle
 2      15       car

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

I tried using to_dict(). But with

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

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 %}
like image 525
user3605780 Avatar asked Nov 27 '16 16:11

user3605780


1 Answers

As John Galt suggested this works:

{% for key,value in x.iterrows() %}
      <option value="{{ value['id'] }}">{{ value['text'] }}</option>
{% endfor %}
like image 112
user3605780 Avatar answered Oct 15 '22 06:10

user3605780