Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display pandas DataFrame into Django template?

I have a pandas DataFrame and I want to show this DataFrame into my Django template. But every time the code compiles successfully without showing any table in my Django web app. What am I missing here? My DataFrame looks like this:

enter image description here

and my html looks like:

<div class="row" style="margin-bottom: 20px;">
        <div class="col-md-3">

        </div>
        <div class="col-md-9">
            <div class="card" style="width: auto;">
                <div class="card-body">
                    <h3 class="h3">Total match data and Profit Mergin</h3>
                    <br>
                    <table class="table table-striped">
                        <tr>
                            <th>Total Contest</th>
                            <th>Total Entry Amount</th>
                            <th>Total Seat</th>
                            <th>Total Winning Amount</th>
                            <th>Total Team Capacity</th>
                            <th>Profit Mergin</th>

                        {% for value in final_data.iterrows %}
                            
                        <tr>
                            <td>{{ value.0 }}</td>
                            <td>{{ value.1 }}</td>
                            <td>{{ value.2 }}</td>
                            <td>{{ value.3 }}</td>
                            <td>{{ value.4 }}</td>
                            <td>{{ value.5 }}</td>
                        </tr>
                        {% endfor %}
                    </table>
                </div>
            </div>
        </div>
    </div>

I tried final_data.iterrows but still got the same result. What should I do now?

like image 390
fa2007 Avatar asked Sep 17 '25 07:09

fa2007


1 Answers

This a basic html table that shows a pandas dataframe in a django template:

<table>
  <tr>
    {% for col in df.columns %}
      <td>
        {{col}}
      </td>
    {% endfor %}
  </tr>
    {% for index, row in df.iterrows %}
      <tr>
        {% for cell in row %}
          <td>
            {{cell}}
          </td>
        {% endfor %}
      </tr>
    {% endfor %}
</table>

This one is not showing the index, so if you want to render it, you could pass df.reset_index() to the template.

like image 199
Pablo Guerrero Avatar answered Sep 19 '25 20:09

Pablo Guerrero