Learn practical skills, build real-world projects, and advance your career

United States Presidential Elections: an Analytical Exploration

Every four years, the United States has a Presidential Election with 2020 being possibly one of the most controversial elections in history. Only two candidates, one from the Republican and one from the Democratic party, have a chance of being elected, but less than 30 days before the election the election outcome is still unsure. I decided to take a dive into the past 40 years of Presidential Elections, in order to determine if there were any trends to be found. I found my data on Kaggle [https://www.kaggle.com/tunguz/us-elections-dataset?select=2016-precinct-president.csv]. I also merged commonly found data that I compiled myself for the winner of each election. I plan to first look at the data to get a better understanding of what we're working with and then use this data to answer questions I've devised that might help us draw conclusions on the outcome of the election.

Prior to taking this Zero to Pandas class, I had almost zero knowledge of Python. Through the course, I've learned basics on the "language" and then have even made it far enough to create data visualizations. The data below will showcase some of the knowledge I've gained in the past 6 weeks from this course.

How to run the code

This is an executable Jupyter notebook hosted on Jovian.ml, a platform for sharing data science projects. You can run and experiment with the code in a couple of ways: using free online resources (recommended) or on your own computer.

Option 1: Running using free online resources (1-click, recommended)

The easiest way to start executing this notebook is to click the "Run" button at the top of this page, and select "Run on Binder". This will run the notebook on mybinder.org, a free online service for running Jupyter notebooks. You can also select "Run on Colab" or "Run on Kaggle".

Option 2: Running on your computer locally
  1. Install Conda by following these instructions. Add Conda binaries to your system PATH, so you can use the conda command on your terminal.

  2. Create a Conda environment and install the required libraries by running these commands on the terminal:

conda create -n zerotopandas -y python=3.8 
conda activate zerotopandas
pip install jovian jupyter numpy pandas matplotlib seaborn opendatasets --upgrade
  1. Press the "Clone" button above to copy the command for downloading the notebook, and run it on the terminal. This will create a new directory and download the notebook. The command will look something like this:
jovian clone notebook-owner/notebook-id
  1. Enter the newly created directory using cd directory-name and start the Jupyter notebook.
jupyter notebook

You can now access Jupyter's web interface by clicking the link that shows up on the terminal or by visiting http://localhost:8888 on your browser. Click on the notebook file (it has a .ipynb extension) to open it.

Downloading the Dataset

First we must locate our data - I used Kaggle to find my original datasource. I downloaded it from Kaggle and uploaded it into this environment, so the data is already in this notebook. I also needed a quick dataset that determined the winner of each election, as this was not included in the data I found. I compiled this quickly from a google search and also uploaded this data into the workbook.

Let's import our data:

import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline