user13861994
user13861994

Reputation:

I am trying to show my DataFrame in a html table using Django but it's only showing column names and not the values in rows?

The file below is my html file , I am trying to show any randomly chosen dataset selected on click of a button in a html table but its only showing column names of the dataframe and not displaying values in the rows? I don't understand why its happening.enter image description here


This is Home.html

 <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Detect Outliers</title>
    </head>
    <body>
        <button type="button">Upload</button> 
        <h1>Uploaded Data</h1> 
        <table border="1px"> 
            <tr> 
                {% for data in DataFrame %} 
                    <th>{{ data }}</th> 
                {% endfor %} 
         
                {% for _, record in DataFrame.iterrows %} 
                    <tr> 
                        {% for value in data %} 
                            <td>{{ value }}</td> 
                        {% endfor %} 
                    </tr> 
                {% endfor %} 
            </tr> 
        </table> 
    </body>
    </html>

This is views.py

from django.shortcuts import render
import pandas as pd 

 
def home(request): 
    dataFrame = pd.read_csv('Bihar_thresholding.csv') 
     
    return render(request, 'home.html', {'DataFrame': dataFrame }) 

This is urls.py

  from django.contrib import admin
    from django.urls import path
    from . import views
    
    urlpatterns = [
    path('', views.home),
]

Upvotes: 0

Views: 488

Answers (2)

user13861994
user13861994

Reputation:

<table border="2px"> 
        <tr> 
            {% for value in DataFrame %} 
                <th>{{ value }}</th>
            {% endfor %} 

            {% for _,row in DataFrame.iterrows %} 
                <tr> 
                    {% for value in row %} 
                        <td>{{ value }}</td> 
                    {% endfor %} 
                </tr> 
            {% endfor %} 
        </tr> 

    </table>

This solved my problem.

Upvotes: 0

snatchysquid
snatchysquid

Reputation: 1352

have you considered using pandas to_html? please read this documentation.

a simple example can be found here.

Upvotes: 1

Related Questions