Skip to content

Commit

Permalink
Merge pull request #2 from dhmit/seatshares
Browse files Browse the repository at this point in the history
Integrated party seat shares in Lok Sabha dataset

**Changes made:**
- Created a model to retrieve seat shares of each party in each election year
- Added raw dataset of BJP and INC seat shares in Lok Sahbha from 1962-2019

**Adjustments to be made:**
- Update raw dataset to include a few more relevant parties and only include data from the past 20 years
  • Loading branch information
JusticeV452 authored Mar 7, 2024
2 parents 963e9f6 + 45ed547 commit 33b66ab
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 4 deletions.
17 changes: 15 additions & 2 deletions backend/app/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
from rest_framework.response import Response

from .models import (
TCPDElection
TCPDElection,
SeatShare
)

from .serializers import (
TCPDElectionSerializer
TCPDElectionSerializer,
SeatShareSerializer
)


Expand All @@ -18,3 +20,14 @@ def all_elections(request):
elections = TCPDElection.objects.all()
serializer = TCPDElectionSerializer(elections, many=True)
return Response(serializer.data)


@api_view(['GET'])
def all_seats(request):
"""
API endpoint to get the seats shares of each party
in each general election in the database
"""
seat_shares = SeatShare.objects.all()
serializer = SeatShareSerializer(seat_shares, many=True)
return Response(serializer.data)
26 changes: 26 additions & 0 deletions backend/app/data/TCPD_GE_LS_seatShare_1962-2019.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"Assembly_No","Deposit_Lost","Election_Type","Expanded_Party_Name","Party","Seat_Share","State_Name","Strike_Rate","Total_Candidates","Total_Seats_in_Assembly","Total_Votes_in_Assembly","Total_Votes_in_Contested_Seats","Vote_Share_in_Assembly","Vote_Share_in_Contested_Seats","Winners","Year","position"
"3","","GE","INDIA NATIONAL CONGRESS","INC","73.08","Lok_Sabha","73.9754098360656","488","494","115168890","19448378","44.72","264.85","361","1962","1"
"4","","GE","INDIA NATIONAL CONGRESS","INC","54.42","Lok_Sabha","54.8449612403101","516","520","145866510","23500701","40.78","253.14","283","1967","1"
"5","","GE","INDIA NATIONAL CONGRESS","INC","67.95","Lok_Sabha","79.8185941043084","441","518","146602276","21918270","43.68","292.15","352","1971","1"
"6","","GE","INDIA NATIONAL CONGRESS","INC","28.41","Lok_Sabha","31.3008130081301","492","542","188917504","29735972","34.52","219.3","154","1977","2"
"7","","GE","INDIAN NATIONAL CONGRESS (I)","INC(I)","66.73","Lok_Sabha","71.7479674796748","492","529","197824274","31396014","42.69","269","353","1980","1"
"8","","GE","BHARATIYA JANATA PARTY","BJP","0.37","Lok_Sabha","0.873362445414847","229","541","249585334","29518632","7.4","62.56","2","1984","13"
"8","","GE","INDIA NATIONAL CONGRESS","INC","76.52","Lok_Sabha","80.0773694390716","517","541","249585334","37526393","48.12","320.06","414","1984","1"
"9","","GE","BHARATIYA JANATA PARTY","BJP","16.07","Lok_Sabha","37.7777777777778","225","529","300776366","33925612","11.36","100.72","85","1989","3"
"9","","GE","INDIA NATIONAL CONGRESS","INC","37.24","Lok_Sabha","38.6274509803922","510","529","300776366","45176688","39.53","263.18","197","1989","1"
"10","","GE","BHARATIYA JANATA PARTY","BJP","22.47","Lok_Sabha","25.1572327044025","477","534","278223387","41689683","20.07","133.95","120","1991","2"
"10","","GE","INDIA NATIONAL CONGRESS","INC","45.69","Lok_Sabha","48.8","500","534","278223387","40759067","36.4","248.5","244","1991","1"
"11","","GE","BHARATIYA JANATA PARTY","BJP","29.65","Lok_Sabha","34.1825902335456","471","543","334873286","50395169","20.29","134.84","161","1996","1"
"11","","GE","INDIA NATIONAL CONGRESS","INC","25.78","Lok_Sabha","26.4650283553875","529","543","334873286","50156345","28.8","192.31","140","1996","2"
"12","","GE","BHARATIYA JANATA PARTY","BJP","33.52","Lok_Sabha","46.9072164948454","388","543","368376700","57322026","25.59","164.45","182","1998","1"
"12","","GE","INDIA NATIONAL CONGRESS","INC","25.97","Lok_Sabha","29.559748427673","477","543","368376700","54083135","25.82","175.86","141","1998","2"
"13","","GE","BHARATIYA JANATA PARTY","BJP","33.52","Lok_Sabha","53.6873156342183","339","543","364437294","54085266","23.75","160.05","182","1999","1"
"13","","GE","INDIA NATIONAL CONGRESS","INC","20.99","Lok_Sabha","25.1655629139073","453","543","364437294","52275259","28.3","197.26","114","1999","2"
"14","","GE","BHARATIYA JANATA PARTY","BJP","25.41","Lok_Sabha","37.9120879120879","364","543","389779784","55253498","22.16","156.32","138","2004","2"
"14","","GE","INDIA NATIONAL CONGRESS","INC","26.7","Lok_Sabha","34.7721822541966","417","543","389779784","55850189","26.53","185.15","145","2004","1"
"15","","GE","BHARATIYA JANATA PARTY","BJP","21.36","Lok_Sabha","26.7898383371824","433","543","417159281","59882187","18.8","130.98","116","2009","2"
"15","","GE","INDIA NATIONAL CONGRESS","INC","37.94","Lok_Sabha","46.8181818181818","440","543","417159281","55227094","28.55","215.67","206","2009","1"
"16","","GE","BHARATIYA JANATA PARTY","BJP","51.93","Lok_Sabha","65.8878504672897","428","543","553802946","80362865","31","213.61","282","2014","1"
"16","","GE","INDIA NATIONAL CONGRESS","INC","8.1","Lok_Sabha","9.48275862068965","464","543","553802946","81994547","19.31","130.42","44","2014","2"
"17","","GE","BHARATIYA JANATA PARTY","BJP","55.8","Lok_Sabha","69.4954128440367","436","543","614163406","87510446","37.3","261.77","303","2019","1"
"17","","GE","INDIA NATIONAL CONGRESS","INC","9.58","Lok_Sabha","12.3515439429929","421","543","614163406","82896328","19.46","144.15","52","2019","2"
49 changes: 49 additions & 0 deletions backend/app/management/commands/load_seatshare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
Django management command load_dataset
Updates local db with values from base csv dataset
"""

import os
import pandas

from django.conf import settings
from django.core.management.base import BaseCommand
from app.models import SeatShare


class Command(BaseCommand):
"""
Custom django-admin command used to run an analysis from the app/analysis folder
"""

help = ""

def add_arguments(self, parser):
parser.add_argument(
"dataset_name",
type=str,
action="store",
help="Name of dataset in app/data folder (with extension)",
)

def handle(self, *args, **options):
# pylint: disable=too-many-locals
file_name = options.get("dataset_name")
file_path = os.path.join(settings.DATASET_DIR, file_name)
df = pandas.read_csv(file_path)

# TODO: Generalize this to update correct model(s) and columns based on dataset
for year, seats_held, total_seats, party_name in zip(
df["Year"],
df["Total_Candidates"],
df["Total_Seats_in_Assembly"],
df["Party"],
):
seat_share = SeatShare(
election_year=year,
seats_held=seats_held,
total_seats=total_seats,
party_name=party_name,
)
seat_share.save()
31 changes: 31 additions & 0 deletions backend/app/migrations/0002_seatshare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 5.0.2 on 2024-03-01 21:42

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("app", "0001_initial"),
]

operations = [
migrations.CreateModel(
name="SeatShare",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("party_name", models.CharField(max_length=10)),
("seats_held", models.IntegerField()),
("election_year", models.IntegerField()),
("total_seats", models.IntegerField()),
],
),
]
20 changes: 20 additions & 0 deletions backend/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@


class TCPDElection(models.Model):
"""
Represents each assembly and general election from 1951-1962
"""
# https://docs.djangoproject.com/en/5.0/ref/models/fields/#enumeration-types
class ElectionType(models.TextChoices):
AE = "AE"
Expand All @@ -17,3 +20,20 @@ class ElectionType(models.TextChoices):
)

number_of_seats = models.IntegerField()


class SeatShare(models.Model):
"""
Represents a political party and the number of seats
it held in Lok Sahbha in a specific election year
"""
# https://docs.djangoproject.com/en/5.0/ref/models/fields/#enumeration-types

party_name = models.CharField(
max_length=10,

)

seats_held = models.IntegerField()
election_year = models.IntegerField()
total_seats = models.IntegerField()
20 changes: 18 additions & 2 deletions backend/app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

from rest_framework import serializers
from .models import (
TCPDElection
TCPDElection,
SeatShare
)


Expand All @@ -16,7 +17,7 @@ class TCPDElectionSerializer(serializers.ModelSerializer):
"""
number_of_seats = serializers.SerializerMethodField()

## TODO: Remove
# TODO: Remove
# Not necessary, done to show how instance variables can be modified upon serialization
@staticmethod
def get_number_of_seats(instance):
Expand All @@ -27,3 +28,18 @@ class Meta:
fields = [
"election_type", "number_of_seats"
]


class SeatShareSerializer(serializers.ModelSerializer):
"""
Serializes seat shares
"""

class Meta:
model = SeatShare
fields = [
"election_year",
"party_name",
"seats_held",
"total_seats"
]
1 change: 1 addition & 0 deletions backend/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@
# API endpoints
################################################################################
path('1951-1962elections/', api_views.all_elections),
path('1962-2019seats/', api_views.all_seats),
]

0 comments on commit 33b66ab

Please sign in to comment.