Skip to content

Use Python to automate the vote-counting process for a local election

Notifications You must be signed in to change notification settings

kobertlam/Election_Analysis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Election_Analysis

Overview of Election Audit

A Colorado Board of Elections employee has given you the following tasks to complete the election audit of a recent local congressional election.

  • Calculation the total number of votes cast.
  • Get the voter turnout for each county.
  • Calculate the total number of votes, and percentage of votes from each county out of the total count.
  • Determine the county with the highest turnout.
  • Get a complete list of candidates who received votes.
  • Calculate the total number of votes each candidate received.
  • Calculate the percentage of votes each candidate won.
  • Determine the winner of the election based on popular vote.

Election Audit Results

Election results output to Terminal Election Results from Terminal

Election Results in TXT file

  • How many votes were cast in this congressional election?

    • There were 369,711 votes cast in the election.
  • Provide a breakdown of the number of votes and the percentage of total votes for each county in the precinct.

    • County Jefferson received 38,855 number of votes and 10.5% out of the total votes.
    • County Denver received 306,055 number of votes and 82.8% out of the total votes.
    • County Arapahoe received 24,801 number of votes and 6.7% out of the total votes.
  • Which county had the largest number of votes?

    • Denver had the largest number of votes.
  • Provide a breakdown of the number of votes and the percentage of the total votes each candidate received.

    • Candidate Charles Casper Stockham received 23.0% of the votes and 85,213 number of votes.
    • Candidate Diana DeGette received 73.8% of the votes and 272,892 number of votes.
    • Candidate Raymon Anthony Doane received 3.1% of the votes and 11,606 number of votes.
  • Which candidate won the election, what was their vote count, and what was their percentage of the total votes?

    • Candidate Diana DeGette, who received 73.81% of the votes and 272,892 number of votes.

Election Audit Summary

  • The script PyPoll_Challenge.py we developed in this analysis can be modified so that it can be used in other elections. Here we may generalize the script to analyse election results for different states:
    1. Retrieve additional "State" information from the source file.
      • Assume the source file election_results.csv also included the "State" information at the second column (i.e. between the "Ballot ID" and "County")
      • The list index for reading data from the source file cen be modifed according to the new CSV data structure.
        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
        
                # Extract the state name from each row.
                state_name = row[1]
                # Extract the county name from each row.
                county_name = row[2]
                # Get the candidate name from each row.
                candidate_name = row[3]
        
    2. Use a list of dictionaries to keep and manipulate the vote information
      • Create a list of dictionaries state_county_candidate_data = [] where the keys are "state", "county", "candidate" and "counts", and each state and each county and each candidate, and the corresponding vote counts are the values for those keys.
      • In each row read from the CSV data file:
        • If the current state and county and candidate is NOT in the list state_county_candidate_data, append a new record to the list.
          state_county_candidate_data.append({"state":state_name, "county":county_name, "candidate":candidate_name, "counts":0})
          
        • Add a vote to that state-county-candidate's vote count.
          # "i" is the list index
          state_county_candidate_data[i]["counts"] += 1
          
      • Use for-loop to get the state list from the list state_county_candidate_data:
        • For each state:
          • Use for-loop to get the county list of this state.
          • For each county:
            • Retrieve the candidate name and the vote count for that candidate in that county.
            • Calculate the votes as a percentage of the total votes of that county.
            • Print the county-candidate results to the terminal and output file.
            • Print the winning candidate information for this county.
          • Retrieve the vote counts in that state.
          • Calculate the votes as a percentage of the total votes of all the states.
          • Print the results to the terminal and output file.

About

Use Python to automate the vote-counting process for a local election

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages