{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Nat Scott"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Research question/interests\n",
"\n",
"**Is there a correlation between political alignment & living in neighbourhoods with large quantities of LGBT people?** The obvious answer to this question is \"yes, they are going to mostly be democrats\" but anyone who's ever been around queer people will know that this question is quite a bit more nuanced than that, and this nuance is what we hope to capture in investigating this question.\n",
"\n",
"- The gaybourhoods data set does not include data on residents political alignments, however, there is a wealth of electoral data available freely online that we intend on incorporating into this project. The primary difficulty then will be developing a geographic \"compatibility layer\" between the data sets so that the data can be understood in the same context. To build this, we intend on working with the OpenStreetMap API to create an additional column representing observations position space in a more neutral way, such as their coordinates.\n",
"- Alternatively, we've also considered working with an additional data set that links US zip codes to their longitude and lattitude positions. As such, incorporating this data would be as easy as merging the two tables.\n",
"\n",
"\n",
"**Is there a correlation between geographical stratums & being LGBT?** This question is more abstract, and will serve as a preliminary exploration of the data in hopes of establishing two key details along the way that will shape the rest of the project: how do we quantify queerness, and how do we best represent it visually?\n",
"\n",
"- Once again, representing this data visually will require determining the coordinates associated with each observation.\n",
"- The gaybourhoods data set defines a \"gaybourhood index\" which effectively measures how friendly a given neighbourhood is to queer people. Since this index is entirely subjective, we will need to closely evaluate it's usefulness for our project and investigate different ways to quantify \"queer-friendliness\"\n",
"- In addition to the last point, since, of course, no matter what choice of observations we make, the measurement will still be subjective, answering this research question will come more so in the form of comparing and contrasting different measurements to see what they tell us.\n",
"- Obviously, visualizing this among many aspects of the other research questions would involve projecting the data onto a map of the United States, so visualizing this research question would motivate many of the visualizations for other components of this project"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import seaborn as sns"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Data Wrangling"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" name | \n",
" lat | \n",
" long | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Hancock OH | \n",
" 41.000471 | \n",
" -83.666033 | \n",
"
\n",
" \n",
" 1 | \n",
" Stafford VA | \n",
" 38.413261 | \n",
" -77.451334 | \n",
"
\n",
" \n",
" 2 | \n",
" Webster NE | \n",
" 40.180646 | \n",
" -98.498590 | \n",
"
\n",
" \n",
" 3 | \n",
" Dimmit TX | \n",
" 28.423587 | \n",
" -99.765871 | \n",
"
\n",
" \n",
" 4 | \n",
" Cedar IA | \n",
" 41.772360 | \n",
" -91.132610 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" name lat long\n",
"0 Hancock OH 41.000471 -83.666033\n",
"1 Stafford VA 38.413261 -77.451334\n",
"2 Webster NE 40.180646 -98.498590\n",
"3 Dimmit TX 28.423587 -99.765871\n",
"4 Cedar IA 41.772360 -91.132610"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"## counties - Relating US counties to their long/lat position on the Earth\n",
"counties = pd.read_csv(\"../data/raw/us-county-boundaries.csv\", sep=\";\")\n",
"\n",
"counties = counties.rename({\n",
" \"NAME\": \"name\",\n",
" \"INTPTLAT\": \"lat\",\n",
" \"INTPTLON\": \"long\",\n",
"}, axis=\"columns\")\n",
"\n",
"# Combine the county name with the state code\n",
"def combine_name_state(row):\n",
" row[\"name\"] = f\"{row['name']} {row['STUSAB']}\"\n",
" return row\n",
"\n",
"counties = counties.apply(combine_name_state, axis=\"columns\")\n",
"\n",
"# We don't need this column anymore\n",
"counties = counties.drop([\"STUSAB\"], axis=\"columns\")\n",
"\n",
"counties.to_csv(\"../data/processed/us-county-boundaries.csv\")\n",
"counties.head()"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" county | \n",
" party | \n",
" votes | \n",
" total | \n",
" percent | \n",
" lat | \n",
" long | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Autauga AL | \n",
" Democrat | \n",
" 6363 | \n",
" 23932 | \n",
" 0.265878 | \n",
" 32.532237 | \n",
" -86.646439 | \n",
"
\n",
" \n",
" 1 | \n",
" Autauga AL | \n",
" Republican | \n",
" 17379 | \n",
" 23932 | \n",
" 0.726183 | \n",
" 32.532237 | \n",
" -86.646439 | \n",
"
\n",
" \n",
" 2 | \n",
" Autauga AL | \n",
" Other | \n",
" 190 | \n",
" 23932 | \n",
" 0.007939 | \n",
" 32.532237 | \n",
" -86.646439 | \n",
"
\n",
" \n",
" 3 | \n",
" Baldwin AL | \n",
" Democrat | \n",
" 18424 | \n",
" 85338 | \n",
" 0.215894 | \n",
" 30.659218 | \n",
" -87.746067 | \n",
"
\n",
" \n",
" 4 | \n",
" Baldwin AL | \n",
" Republican | \n",
" 66016 | \n",
" 85338 | \n",
" 0.773583 | \n",
" 30.659218 | \n",
" -87.746067 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" county party votes total percent lat long\n",
"0 Autauga AL Democrat 6363 23932 0.265878 32.532237 -86.646439\n",
"1 Autauga AL Republican 17379 23932 0.726183 32.532237 -86.646439\n",
"2 Autauga AL Other 190 23932 0.007939 32.532237 -86.646439\n",
"3 Baldwin AL Democrat 18424 85338 0.215894 30.659218 -87.746067\n",
"4 Baldwin AL Republican 66016 85338 0.773583 30.659218 -87.746067"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"## pol - Election results from the 2012 American presidential election\n",
"pol = pd.read_csv(\"../data/raw/countypres_2000-2020.csv\")\n",
"\n",
"# We only want 2012--the latest election before the gb data was collected\n",
"\n",
"pol = pol[pol[\"year\"] == 2012].reset_index()\n",
"\n",
"# Get rid of undesireable columns\n",
"pol = pol.drop([\n",
" \"year\", \"state\", \"county_fips\", \"office\",\n",
" \"candidate\", \"version\", \"mode\", \"index\",\n",
"], axis=\"columns\")\n",
"\n",
"# Change the column names to make them a little more friendly\n",
"pol.rename({\n",
" \"county_name\": \"county\",\n",
" \"state_po\": \"state\",\n",
" \"candidatevotes\": \"votes\",\n",
" \"totalvotes\": \"total\"\n",
"}, axis=\"columns\", inplace=True)\n",
"\n",
"# Make cells lowercase\n",
"pol[\"county\"] = pol[\"county\"].apply(lambda x: x.capitalize())\n",
"pol[\"party\"] = pol[\"party\"].apply(lambda x: x.capitalize())\n",
"\n",
"# Combine the county name with the state code\n",
"def combine_name_state(row):\n",
" row[\"county\"] = f\"{row['county']} {row['state']}\"\n",
" return row\n",
"\n",
"pol = pol.apply(combine_name_state, axis=\"columns\")\n",
"\n",
"# Add a percent column which will be useful when graphing\n",
"pol[\"percent\"] = pol[\"votes\"] / pol[\"total\"]\n",
"\n",
"# Attach long/lat data to each row\n",
"pol = pol.merge(counties, left_on=\"county\", right_on=\"name\")\n",
"\n",
"# Now we can get rid of the state columns\n",
"pol = pol.drop([\"state\", \"name\"], axis=\"columns\")\n",
"\n",
"pol.to_csv(\"../data/processed/election-2012.csv\", index=False)\n",
"pol.head()"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Tax_Mjoint | \n",
" TaxRate_SS | \n",
" TaxRate_FF | \n",
" TaxRate_MM | \n",
" Cns_RateSS | \n",
" Cns_RateFF | \n",
" Cns_RateMM | \n",
" CountBars | \n",
" FF_Index | \n",
" MM_Index | \n",
" SS_Index | \n",
" TOTINDEX | \n",
" lat | \n",
" long | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 2120 | \n",
" 203.301887 | \n",
" 28.773585 | \n",
" 174.528302 | \n",
" 77.125329 | \n",
" 6.931719 | \n",
" 70.193610 | \n",
" 15 | \n",
" 6.724415 | \n",
" 48.288254 | \n",
" 55.012669 | \n",
" 67.077054 | \n",
" 34.093828 | \n",
" -118.381697 | \n",
"
\n",
" \n",
" 1 | \n",
" 5080 | \n",
" 205.511811 | \n",
" 33.464567 | \n",
" 172.047244 | \n",
" 88.478367 | \n",
" 15.617404 | \n",
" 72.860963 | \n",
" 17 | \n",
" 9.834048 | \n",
" 48.578469 | \n",
" 58.412517 | \n",
" 61.866815 | \n",
" 37.758057 | \n",
" -122.435410 | \n",
"
\n",
" \n",
" 2 | \n",
" 5790 | \n",
" 107.772021 | \n",
" 16.753022 | \n",
" 91.018998 | \n",
" 46.771050 | \n",
" 5.745582 | \n",
" 41.025469 | \n",
" 5 | \n",
" 4.370779 | \n",
" 26.360413 | \n",
" 30.731192 | \n",
" 37.908747 | \n",
" 40.742039 | \n",
" -74.000620 | \n",
"
\n",
" \n",
" 3 | \n",
" 3510 | \n",
" 80.056980 | \n",
" 21.082621 | \n",
" 58.974359 | \n",
" 31.619291 | \n",
" 9.315448 | \n",
" 22.303843 | \n",
" 10 | \n",
" 6.055939 | \n",
" 15.939869 | \n",
" 21.995808 | \n",
" 37.530067 | \n",
" 40.734012 | \n",
" -74.006746 | \n",
"
\n",
" \n",
" 4 | \n",
" 2660 | \n",
" 91.353383 | \n",
" 12.781955 | \n",
" 78.571429 | \n",
" 21.763042 | \n",
" 3.142678 | \n",
" 18.620365 | \n",
" 9 | \n",
" 3.004058 | \n",
" 18.280165 | \n",
" 21.284224 | \n",
" 35.843573 | \n",
" 37.773134 | \n",
" -122.411167 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Tax_Mjoint TaxRate_SS TaxRate_FF TaxRate_MM Cns_RateSS Cns_RateFF \\\n",
"0 2120 203.301887 28.773585 174.528302 77.125329 6.931719 \n",
"1 5080 205.511811 33.464567 172.047244 88.478367 15.617404 \n",
"2 5790 107.772021 16.753022 91.018998 46.771050 5.745582 \n",
"3 3510 80.056980 21.082621 58.974359 31.619291 9.315448 \n",
"4 2660 91.353383 12.781955 78.571429 21.763042 3.142678 \n",
"\n",
" Cns_RateMM CountBars FF_Index MM_Index SS_Index TOTINDEX \\\n",
"0 70.193610 15 6.724415 48.288254 55.012669 67.077054 \n",
"1 72.860963 17 9.834048 48.578469 58.412517 61.866815 \n",
"2 41.025469 5 4.370779 26.360413 30.731192 37.908747 \n",
"3 22.303843 10 6.055939 15.939869 21.995808 37.530067 \n",
"4 18.620365 9 3.004058 18.280165 21.284224 35.843573 \n",
"\n",
" lat long \n",
"0 34.093828 -118.381697 \n",
"1 37.758057 -122.435410 \n",
"2 40.742039 -74.000620 \n",
"3 40.734012 -74.006746 \n",
"4 37.773134 -122.411167 "
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"## gb - the gaybourhoods dataset\n",
"gb = pd.read_csv(\"../data/raw/gaybourhoods.csv\")\n",
"cords = pd.read_csv(\"../data/raw/zip_lat_long.csv\")\n",
"\n",
"# Let's add long/lat columns to gb\n",
"gb = gb.merge(cords, left_on=\"GEOID10\", right_on=\"ZIP\")\n",
"\n",
"# Get rid of unneeded columns\n",
"gb = gb.drop([\n",
" \"Mjoint_MF\", \"Mjoint_SS\", \"Mjoint_FF\", \"Mjoint_MM\",\n",
" \"Cns_TotHH\", \"Cns_UPSS\", \"Cns_UPFF\", \"Cns_UPMM\",\n",
" \"ParadeFlag\", \"FF_Tax\", \"FF_Cns\", \"MM_Tax\", \"MM_Cns\",\n",
" \"SS_Index_Weight\", \"Parade_Weight\", \"Bars_Weight\",\n",
" \"GEOID10\", \"ZIP\",\n",
"], axis=\"columns\")\n",
"\n",
"# There's a lot of info baked into some of these columns. Especially the composite indexes.\n",
"# We'll leave their names as is for easy reference even if they're a little ugly.\n",
"gb = gb.rename({\n",
" \"LAT\": \"lat\",\n",
" \"LNG\": \"long\",\n",
"}, axis=\"columns\")\n",
"\n",
"gb.to_csv(\"../data/processed/gaybourhoods-nat.csv\")\n",
"gb.head()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}