-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92b7b4f
commit fe3bdd2
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
hub/management/commands/import_hftf_trained_constituents.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from django.conf import settings | ||
|
||
import pandas as pd | ||
|
||
from hub.models import AreaData, DataSet | ||
|
||
from .base_importers import BaseImportFromDataFrameCommand | ||
|
||
|
||
class Command(BaseImportFromDataFrameCommand): | ||
help = "Import data about number of active HFTF constituents per constituency" | ||
|
||
data_file = ( | ||
settings.BASE_DIR | ||
/ "data" | ||
/ "HFTF_ Project Groundgame data 07_02_2023.xlsx - Main Sheet.csv" | ||
) | ||
cons_row = "Constituency" | ||
message = "Importing HFTF trained constituents data" | ||
uses_gss = False | ||
|
||
defaults = { | ||
"data_type": "integer", | ||
"category": "movement", | ||
"subcategory": "supporters_and_activists", | ||
"description": "Number of constituents trained by Hope For The Future per constituency.", | ||
"release_date": "February 2023", | ||
"source_label": "Data from Hope For The Future.", | ||
"source": "https://www.hftf.org.uk/", | ||
"source_type": "google sheet", | ||
"table": "areadata", | ||
"default_value": 1, | ||
"data_url": "", | ||
"unit_type": "raw", | ||
"unit_distribution": "people_in_area", | ||
"comparators": DataSet.numerical_comparators(), | ||
} | ||
|
||
data_sets = { | ||
"hftf_constituents_count": { | ||
"defaults": defaults, | ||
"col": "constituents_count", | ||
}, | ||
} | ||
|
||
def get_dataframe(self): | ||
df = pd.read_csv( | ||
self.data_file, | ||
usecols=["Constituency", "Group / Individual"], | ||
) | ||
df = ( | ||
df.dropna(subset="Constituency") | ||
.groupby("Constituency") | ||
.count()["Group / Individual"] | ||
.reset_index() | ||
.rename(columns={"Group / Individual": "constituents_count"}) | ||
) | ||
df.constituents_count = df.constituents_count.astype(int) | ||
return df | ||
|
||
def get_label(self, defaults): | ||
return "Number of trained HFTF constituents" | ||
|
||
def delete_data(self): | ||
AreaData.objects.filter(data_type__in=self.data_types.values()).delete() |