Skip to content

Determined the results of an election as well as voter participation using Python script.

Notifications You must be signed in to change notification settings

java2509/Election_Analysis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

election_analysis

Election Audit Analysis for the Colorado Board of Election.

thinkstock-colorado-vote-election_1200xx3751-2110-0-16

Overview of the Election Audit

Purpose:

Tom is a Colorado board of elections employee who needs assistance with automating the Election audit of the tabulated results for the US Congressional precinct in Colorado.

The US congressional race will be certified by the automated election audit with the following results:

  • The total number of votes cast.
  • A complete list of candidates who received votes and the counties that voted.
  • The total number of votes each candidate received.
  • The percentage of votes each candidate won.
  • The county with the highest voter turnout.
  • The winning candidate of the election which is based on the popular vote.

Resources:

Python Version 3.7.6

VS Code Version 1.62.3

Data Election Results

Election Audit Results:

The analysis of the automated process shows the follwing results

  • There were 369,711 votes cast in the election.
  • The results for each county were:
    • Jefferson county cast 38,855 votes, 10.5% of the total vote.
    • Denever county cast 306,055 votes, 82.8% of the total vote.
    • Arapahoe county cast 24,801 votes, 6.7% of the total vote.
  • The county with the largest number of votes was Denver.
  • The results for each candidate:
    • Charles Casper Stockham received 23% of the vote and 85,213 number of votes.
    • Diana DeGette received 73.8% of the vote and 272,892 number of votes.
    • Raymon Anthony Doane received 3.1% of the vote with 11,606 number of votes.
  • The winner of the election was Diana DeGette who received 73.8% of the votes and 272,892 number of votes.

D09B093C-769A-4C36-9ABC-91B7E52E8158

Election Audit Summary:

This automated process could also be used for primary,general and local elections, some modifications would be have to made for the following:

  • Change county list to state list for a primary, general or local election
  • The amount of votes for each candidate by county could be determined with modification to the code by adding in another if loop.

Code:

 -*- coding: UTF-8 -*-
"""PyPoll Homework Challenge Solution."""

- Add our dependencies.
    import csv
import os

# Add a variable to load a file from a path.
    file_to_load = os.path.join("Resources", "election_results.csv")

# Add a variable to save the file to a path.
file_to_save = os.path.join("analysis", "election_analysis.txt")

# Initialize a total vote counter.
total_votes = 0

# Candidate Options and candidate votes.
candidate_options = []
candidate_votes = {}

# 1: Create a county list and county votes dictionary.
county_list = []
county_votes = {}

# Track the winning candidate, vote count and percentage
winning_candidate = ""
winning_count = 0
winning_percentage = 0

# 2: Track the largest county and county voter turnout.
county_largest = ""
county_turnout = 0
vote_percentage = 0

# Read the csv and convert it into a list of dictionaries
with open(file_to_load) as election_data:
    reader = csv.reader(election_data)

    # Read the header
    header = next(reader)

    # For each row in the CSV file.
    for row in reader:

        # Add to the total vote count
        total_votes = total_votes + 1

        # Get the candidate name from each row.
        candidate_name = row[2]

        # 3: Extract the county name from each row.
        county_name = row[1]

        # If the candidate does not match any existing candidate add it to
        # the candidate list
        if candidate_name not in candidate_options:

            # Add the candidate name to the candidate list.
            candidate_options.append(candidate_name)

            # And begin tracking that candidate's voter count.
            candidate_votes[candidate_name] = 0

        # Add a vote to that candidate's count
        candidate_votes[candidate_name] += 1

        # 4a: Write an if statement that checks that the
        # county does not match any existing county in the county list.
        if county_name not in county_list:

            # 4b: Add the existing county to the list of counties.
            county_list.append(county_name)

            # 4c: Begin tracking the county's vote count.
            county_votes[county_name]=0

        # 5: Add a vote to that county's vote count.
        county_votes[county_name] += 1

# Save the results to our text file.
with open(file_to_save, "w") as txt_file:

    # Print the final vote count (to terminal)
    election_results = (
        f"\nElection Results\n"
        f"-------------------------\n"
        f"Total Votes: {total_votes:,}\n"
        f"-------------------------\n\n"
        f"County Votes:\n")

    print(election_results, end="")

    txt_file.write(election_results)

    # 6a: Write a for loop to get the county from the county dictionary.
    for county_name in county_votes:
        # 6b: Retrieve the county vote count.
        votes = county_votes[county_name]
        # 6c: Calculate the percentage of votes for the county.
        vote_percentage = (votes/total_votes) * 100
         # 6d: Print the county results to the terminal.
        vote_results = (f"{county_name}: {vote_percentage:.1f}% ({votes:,})\n")
        print(vote_results)
         # 6e: Save the county votes to a text file.
        txt_file.write(vote_results)

         # 6f: Write an if statement to determine the winning county and get its vote count.
        if (votes > county_turnout):
            county_largest = county_name
            county_turnout = votes
       
    # 7: Print the county with the largest turnout to the terminal.
    county_largest_summary = (
        f"\n"
        f"-------------------------\n"
        f"Largest County Turnout: {county_largest}\n"
        f"-------------------------\n")
    print(county_largest_summary)

    # 8: Save the county with the largest turnout to a text file.

    txt_file.write(county_largest_summary)

    # Save the final candidate vote count to the text file.
    for candidate_name in candidate_votes:

        # Retrieve vote count and percentage
        votes = candidate_votes.get(candidate_name)
        vote_percentage = float(votes) / float(total_votes) * 100

        # Print each candidate's voter count and percentage to the terminal.
        candidate_results_summary = (
            f"{candidate_name}: {vote_percentage:.1f}% ({votes:,})\n")
        print(candidate_results_summary)

        #  Save the candidate results to our text file.
        txt_file.write(candidate_results_summary)

        # Determine winning vote count, winning percentage, and candidate.
        if (votes > winning_count) and (vote_percentage > winning_percentage):
            winning_count = votes
            winning_candidate = candidate_name
            winning_percentage = vote_percentage

    # Print the winning candidate (to terminal)
    winning_candidate_summary = (
        f"-------------------------\n"
        f"Winner: {winning_candidate}\n"
        f"Winning Vote Count: {winning_count:,}\n"
        f"Winning Percentage: {winning_percentage:.1f}%\n"
        f"-------------------------\n")
    print(winning_candidate_summary)

    # Save the winning candidate's name to the text file
    txt_file.write(winning_candidate_summary)

About

Determined the results of an election as well as voter participation using Python script.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages