-
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.
helpful for turning CSVs with multuple rows per constituency into ones with multuple columns per constituency
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
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,48 @@ | ||
import json | ||
|
||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
|
||
import pandas as pd | ||
|
||
|
||
class Command(BaseCommand): | ||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--infile", | ||
action="store", | ||
help="File to process", | ||
) | ||
|
||
parser.add_argument( | ||
"--outfile", | ||
action="store", | ||
help="File to output to", | ||
) | ||
|
||
parser.add_argument( | ||
"--column", | ||
action="store", | ||
help="Column to pivot on", | ||
) | ||
|
||
parser.add_argument( | ||
"--value_column", | ||
action="store", | ||
help="Column with values", | ||
) | ||
|
||
parser.add_argument( | ||
"--index", | ||
action="store", | ||
help="Column with index", | ||
) | ||
|
||
def handle(self, *args, **kwargs): | ||
df = pd.read_csv(settings.BASE_DIR / "data" / kwargs["infile"]) | ||
df = df.pivot( | ||
columns=kwargs["column"], | ||
values=kwargs["value_column"], | ||
index=kwargs["index"], | ||
) | ||
df.to_csv(settings.BASE_DIR / "data" / kwargs["outfile"]) |