Remove winning party
This commit is contained in:
parent
8614acdfae
commit
a98c54c5b8
12 changed files with 36 additions and 89 deletions
|
|
@ -8,7 +8,7 @@ import polars as pl
|
|||
# One row per candidate per constituency — we aggregate to per-constituency stats.
|
||||
URL = "https://electionresults.parliament.uk/general-elections/6/candidacies.csv"
|
||||
|
||||
# Map party names to a smaller set for the enum feature and vote share columns.
|
||||
# Map party names to a smaller set for vote share columns.
|
||||
# Only parties that won seats in England are kept; the rest become "Other parties".
|
||||
PARTY_MAP = {
|
||||
"Labour": "Labour",
|
||||
|
|
@ -37,10 +37,9 @@ def download_and_convert(output_path: Path) -> None:
|
|||
.alias("party_group"),
|
||||
)
|
||||
|
||||
# ── Per-constituency winner stats ──
|
||||
winners = df.filter(pl.col("Candidate result position") == 1).select(
|
||||
# ── Per-constituency turnout ──
|
||||
turnout = df.filter(pl.col("Candidate result position") == 1).select(
|
||||
pl.col("Constituency geographic code").alias("pcon"),
|
||||
pl.col("party_group").alias("winning_party"),
|
||||
(pl.col("Election valid vote count") / pl.col("Electorate") * 100)
|
||||
.round(1)
|
||||
.alias("turnout_pct"),
|
||||
|
|
@ -72,14 +71,11 @@ def download_and_convert(output_path: Path) -> None:
|
|||
rename_map = {col: f"% {col}" for col in party_pct.columns if col != "pcon"}
|
||||
party_pct = party_pct.rename(rename_map)
|
||||
|
||||
# Join winner stats with party vote shares
|
||||
result = winners.join(party_pct, on="pcon", how="left")
|
||||
# Join turnout with party vote shares
|
||||
result = turnout.join(party_pct, on="pcon", how="left")
|
||||
|
||||
print(f"Constituencies: {result.height}")
|
||||
print(f"Columns: {result.columns}")
|
||||
print(
|
||||
f"Party breakdown:\n{result['winning_party'].value_counts().sort('count', descending=True)}"
|
||||
)
|
||||
|
||||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
result.write_parquet(output_path, compression="zstd")
|
||||
|
|
|
|||
|
|
@ -63,7 +63,6 @@ _AREA_COLUMNS = [
|
|||
# Demographics
|
||||
"Median age",
|
||||
# Politics
|
||||
"Winning party",
|
||||
"Voter turnout (%)",
|
||||
"% Labour",
|
||||
"% Conservative",
|
||||
|
|
@ -361,7 +360,6 @@ def _build(
|
|||
"floor_height": "Interior height (m)",
|
||||
"was_council_house": "Former council house",
|
||||
"median_age": "Median age",
|
||||
"winning_party": "Winning party",
|
||||
"turnout_pct": "Voter turnout (%)",
|
||||
}
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue