How To Use Pandas Library in Django with Examples

Pandas and Django are two powerful Python libraries widely used in data analysis and web development, respectively. Combining their capabilities can greatly enhance the data handling and processing aspects of your Django web application. In this blog, we will explore how to integrate Pandas into a Django project, and we’ll provide a practical example to demonstrate its usage.

Prerequisites:

Before we begin, ensure you have the following prerequisites in place:

  1. Django installed on your system.
  2. Basic Understanding of Pandas Library.
  3. Virtual Environment, this is optional but recommended. You check our blog here.

Note: For this tutorial, we are using our basic skeleton project for Django. We have also create myapp app in this project which we will use for tutorial.

Practical Video Tutorial

Integrating Pandas into Django:

For this tutorial we will be using django-pandas library. The django-pandas library is specifically designed to bridge the gap between Django and Pandas, making it even easier to use Pandas within your Django projects. It provides various utilities and integration points that simplify data processing and analysis tasks.

Advantages of using django-pandas:

  1. Seamless Integration: With django-pandas, you can seamlessly integrate Pandas functionality into your Django models and views, reducing the boilerplate code and streamlining the data processing pipeline.
  2. QuerySet Enhancements: The library extends Django’s QuerySet API, allowing you to perform complex database queries and retrieve data directly as Pandas DataFrames. This makes data retrieval and manipulation much more convenient and efficient.
  3. Serialization Support: django-pandas offers serialization support, enabling you to convert Pandas DataFrames to various formats like JSON, Excel, or CSV, and vice versa, without much effort.
  4. DataFrame Methods as QuerySet Extensions: You can use Pandas DataFrame methods as extensions of Django QuerySets, making it simple to chain Pandas operations with Django queries.
  5. Integration with Admin Interface: The library integrates with Django’s admin interface, allowing you to export data from the database as CSV or Excel files directly from the admin site.

Step 1: Install and import django-pandas

Install the django-pandas library using pip:

pip install django-pandas

Step 2: Add 'django_pandas' to your INSTALLED_APPS

In your Django project’s settings.py, add 'django_pandas' to the INSTALLED_APPS setting:

INSTALLED_APPS = [
    # other apps...
    'django_pandas',
]

Django Pandas Examples

Let’s assume we have a Django model named Employee with the following fields:

# models.py

from django.db import models

class Employee(models.Model):
    name = models.CharField(max_length=100)
    age = models.PositiveIntegerField()
    department = models.CharField(max_length=50)
    salary = models.DecimalField(max_digits=10, decimal_places=2)

1. Django QuerySet to Pandas DataFrame

To convert a Django QuerySet to a Pandas DataFrame, you can use the django_pandas.io.read_frame() function provided by the django-pandas library.

# views.py

from django_pandas.io import read_frame
from django.shortcuts import render
from .models import Employee

def employees_data(request):
    # Retrieve all employees from the database using Django QuerySet
    queryset = Employee.objects.all()

    # Convert the QuerySet to a Pandas DataFrame
    employees_df = read_frame(queryset)

    return render(request, 'employees_data.html', {'employees_df': employees_df})

2. Pandas DataFrame to Django Model

To convert a Pandas DataFrame to Django Model instances and save them in the database, you can use the DataFrame.iterrows() method.

# views.py

from django.shortcuts import render
from .models import Employee
import pandas as pd

def save_employees_data(request):
    # Sample data in a Pandas DataFrame
    data = {
        'name': ['John', 'Alice', 'Bob'],
        'age': [30, 25, 28],
        'department': ['HR', 'Finance', 'Engineering'],
        'salary': [50000.00, 60000.00, 55000.00],
    }

    employees_df = pd.DataFrame(data)

    # Convert the DataFrame to Django Model instances and save them
    for index, row in employees_df.iterrows():
        Employee.objects.create(
            name=row['name'],
            age=row['age'],
            department=row['department'],
            salary=row['salary']
        )

    return render(request, 'success.html')

3. Pandas DataFrame to HTML Table

To convert a Pandas DataFrame to an HTML table, you can use the DataFrame.to_html() method.

# views.py

from django.shortcuts import render
import pandas as pd

def display_table(request):
    # Sample data in a Pandas DataFrame
    data = {
        'name': ['John', 'Alice', 'Bob'],
        'age': [30, 25, 28],
        'department': ['HR', 'Finance', 'Engineering'],
        'salary': [50000.00, 60000.00, 55000.00],
    }

    employees_df = pd.DataFrame(data)

    # Convert the DataFrame to an HTML table
    html_table = employees_df.to_html()

    return render(request, 'display_table.html', {'html_table': html_table})

In these examples, we’ve used sample data for simplicity. However, in a real Django application, you would retrieve data from the database or other sources to create the Pandas DataFrame and perform conversions accordingly.

Ensure you have templates (employees_data.html, success.html, and display_table.html) in the appropriate folder to render the data in your Django views. You can find those html file in Github Folder inside myapp/templates folder.

Find this Project on Github.