26

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 %}
Jamal
  • 763
  • 7
  • 22
  • 32
user3605780
  • 6,542
  • 13
  • 42
  • 67
  • 9
    [`df.iterrows()`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.iterrows.html) is used to Iterate over DataFrame rows as (index, Series) pairs. In place of `x.items()`. – Zero Nov 27 '16 at 16:12
  • Thanks this worked – user3605780 Nov 27 '16 at 16:21
  • Please copy your solution into an answer and then you can accept it yourself. – poolie Nov 27 '16 at 17:59

1 Answers1

39

As John Galt suggested this works:

{% for key,value in x.iterrows() %}
      <option value="{{ value['id'] }}">{{ value['text'] }}</option>
{% endfor %}
user3605780
  • 6,542
  • 13
  • 42
  • 67