{ "cells": [ { "cell_type": "code", "execution_count": 272, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 273, "metadata": {}, "outputs": [], "source": [ "#Import data file\n", "election_path = \"election_data.csv\"" ] }, { "cell_type": "code", "execution_count": 274, "metadata": {}, "outputs": [], "source": [ "#Read in CSV file\n", "election_df = pd.read_csv(election_path, encoding=\"utf-8\")" ] }, { "cell_type": "code", "execution_count": 275, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Voter IDCountyCandidate
012864552MarshKhan
117444633MarshCorrey
219330107MarshKhan
319865775QueenKhan
411927875MarshKhan
\n", "
" ], "text/plain": [ " Voter ID County Candidate\n", "0 12864552 Marsh Khan\n", "1 17444633 Marsh Correy\n", "2 19330107 Marsh Khan\n", "3 19865775 Queen Khan\n", "4 11927875 Marsh Khan" ] }, "execution_count": 275, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Test above statement\n", "election_df.head()" ] }, { "cell_type": "code", "execution_count": 276, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3521001" ] }, "execution_count": 276, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Get the total number of votes cast\n", "Voter_ID = len(election_df[\"Voter ID\"].unique())\n", "Voter_ID" ] }, { "cell_type": "code", "execution_count": 287, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['Khan', 'Correy', 'Li', \"O'Tooley\"], dtype=object)" ] }, "execution_count": 287, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Names of candidates with votes\n", "Candidates = election_df[\"Candidate\"].unique()\n", "Candidates" ] }, { "cell_type": "code", "execution_count": 327, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Khan 63.0\n", "Correy 20.0\n", "Li 14.0\n", "O'Tooley 3.0\n", "Name: Candidate, dtype: float64\n" ] } ], "source": [ "#Candidates by 5 of votes cast\n", "Percent_of_Votes = round((votes/Voter_ID)*100)\n", "print (Percent_of_Votes) " ] }, { "cell_type": "code", "execution_count": 326, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CandidateVotesPercent of Votes
KhanKhan221823163.0
CorreyCorry70420020.0
LiLi49294014.0
O'TooleyO'Tooley1056303.0
\n", "
" ], "text/plain": [ " Candidate Votes Percent of Votes\n", "Khan Khan 2218231 63.0\n", "Correy Corry 704200 20.0\n", "Li Li 492940 14.0\n", "O'Tooley O'Tooley 105630 3.0" ] }, "execution_count": 326, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create data file containing candidate and total number and percent of votes\n", "Election_Results = pd.DataFrame(\n", "{\"Candidate\": [\"Khan\", \"Corry\", \"Li\", \"O'Tooley\"],\n", "\"Votes\": votes,\n", "\"Percent of Votes\":Percent_of_Votes\n", "})\n", "Election_Results" ] }, { "cell_type": "code", "execution_count": 309, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Candidate Votes\n", "Khan Khan 2218231\n" ] } ], "source": [ "#The winner of the election based on popular vote.\n", "Winner = Election_Results[\"Votes\"].max()\n", "print (Election_Results.loc[Election_Results[\"Votes\"]== Winner])\n", "\n" ] }, { "cell_type": "code", "execution_count": 329, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Election Results\n", "________________________________________________\n", "\n", "Total Votes: 3521001\n", "________________________________________________\n", "\n", "Votes by candidates:\n", " Candidate Votes Percent of Votes\n", "Khan Khan 2218231 63.0\n", "Correy Corry 704200 20.0\n", "Li Li 492940 14.0\n", "O'Tooley O'Tooley 105630 3.0\n", "________________________________________________\n", "\n", "Winner:\n", " Candidate Votes Percent of Votes\n", "Khan Khan 2218231 63.0\n" ] } ], "source": [ "#Print results\n", "print (\"Election Results\")\n", "print (\"________________________________________________\")\n", "print (\"\")\n", "print (f\"Total Votes: {(Voter_ID)}\")\n", "print (\"________________________________________________\")\n", "print (\"\")\n", "print (\"Votes by candidates:\")\n", "print (Election_Results)\n", "print (\"________________________________________________\")\n", "print (\"\")\n", "print (\"Winner:\")\n", "print (Election_Results.loc[Election_Results[\"Votes\"]== Winner])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Output Files\n", "\n", "with open(file_to_output, \"w\") as txt_file:\n", "\n", " " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }