Search code examples
pythondjangopandaspython-itertools

Loop Pandas table in Django template


I have a Pandas table which filled with values in my view. This view send this data to my template. Unfortunately I can't loop the values despite I can it in python shell. I attach my table and my attempt:

My table (MyTable):

ID    day      data
_|___________|_____
0| 2017-01-01|100.0|
1| 2017-01-02|99.8 |
2| 2017-01-03|90.0 |

My attempt:

{%for i, b in MyTable.itertools() %}
            <td>{{b['day']}}</td><td> {{b['data']}}</td>
{%endfor%} 

I got the following error message:

Could not parse the remainder: '()' from 'MyTable.iterools()'

In python shell (where I testing) I could loop the table by the method below. How can I loop my pandas table in Django template? Thank you in advance.


Solution

  • As is clearly documented, Django template language doesn't allow () or []. Functions are called automatically and the dot notation is used for item lookups.

    Also, a Pandas datatable doesn't have an itertools method; you probably meant itertuples which works like this:

    {% for b in MyTable.itertuples %}
            <td>{{ b.day }}</td><td> {{ b.data }}</td>
    {% endfor %}