Learn how to use pandas in Django. In this tutorial, you will learn how to use pandas in Django data. And convert a query set of data into a Data frame. Like how you convert a CSV data file into a Data Frame. And perform the data science operation right away in Django Views.

Requirements :

pip install django
pip install pandas

Creating Model for Query Set Data (models.py) :

from django.db import models

# Create your models here.

class Student(models.Model):
    name = models.CharField(max_length=200)
    rollnum = models.IntegerField()
    rank = models.IntegerField()

Register model in Admin page in (admin.py) :

from django.contrib import admin
from django.contrib.admin.decorators import register
from .models import *
# Register your models here.
admin.site.register(Student)

Creating View in (views.py) :

from django.shortcuts import render
from .models import*
import pandas as pd
# Create your views here.
def home(request):
    item = Student.objects.all().values()
    df = pd.DataFrame(item)
    mydict = {
        "df": df.to_html()
    }
    return render(request, 'index.html', context=mydict)

Html Template Code for Data frame display in table format :

<html>
<body>
    {{df|safe}}
</body>
</html>

Output

How To Use Pandas in Django