Using the most timely and accurate data available onhttps://www.census.gov/en.html, write a python script that pulls the following poverty data for Harris County, TX in the year 2018: Estimate of the...


Using the most timely and accurate data available on
https://www.census.gov/en.html, write a python script that pulls the following poverty data for Harris County, TX in the year 2018:



  • Estimate of the number of people under the age of 18 in poverty

  • Estimate of the number of people of any age in poverty

  • Estimate of the median household income


Your script should then write this data to a .csv file, including appropriately named headers.



Requirements:



  • Use an api key to authenticate your requests

  • Use therequestslibrary (no custom libraries that pull Census data)




{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import requests\n", "import urllib.request\n", "import time\n", "from bs4 import BeautifulSoup" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#https://api.census.gov/data/timeseries/poverty/histpov2?get=POV&time=2018&RACE=1&for=county:001∈=state:01&key=979bc0cb7baf0a693771f70bf01c525122a88cde." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "#url='https://api.census.gov/data'" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "#response=requests.get('https://api.census.gov/data')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "200" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#response.status_code" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "HOST = 'https://api.census.gov/data'\n", "Year = '2018'\n", "dataset = \"timeseries/poverty/saipe?\"\n", "base_url = \"/\".join([HOST, Year, dataset])\n", "predicates = {}\n", "get_vars = [\"SAEMHI_PT\", \"SAEPOV0_17_PT\", \"SAEPOVALL_PT\"]\n", "predicates[\"get\"] = \",\".join(get_vars)\n", "predicates[\"for\"] = \"state:48\"\n", "r = requests.get(base_url, params = predicates)\n", "#https://api.census.gov/data/timeseries/poverty/saipe?get=SAEMHI_PT,SAEPOV0_17_PT,SAEPOVALL_PT&for=county:203∈=state:48&time=2018" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HTTP Status 404 - /data/2018/timeseries/poverty/saipe \n" ] } ], "source": [ "print(r.text)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "r = requests.get('https://api.census.gov/data/timeseries/poverty/saipe?get=SAEMHI_PT,SAEPOV0_17_PT,SAEPOVALL_PT&for=county:203∈=state:48&time=2018')\n" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[\"SAEMHI_PT\",\"SAEPOV0_17_PT\",\"SAEPOVALL_PT\",\"time\",\"state\",\"county\"],\n", "[\"50131\",\"3777\",\"9584\",\"2018\",\"48\",\"203\"]]\n" ] } ], "source": [ "print(r.text)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "col_names = [\"Median_household_income\", \"Under_18_in_poverty\", \"All_ages_in_Poverty\", \"Year\", \"State\", \"County\"]" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "df = pd.DataFrame(columns = col_names, data = r.json()[1:])" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Median_household_income Under_18_in_poverty All_ages_in_Poverty Year State \\\n", "0 50131 3777 9584 2018 48 \n", "\n", " County \n", "0 203 \n" ] } ], "source": [ "print(df.head())" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[['SAEMHI_PT', 'SAEPOV0_17_PT', 'SAEPOVALL_PT', 'time', 'state', 'county'], ['50131', '3777', '9584', '2018', '48', '203']]\n" ] } ], "source": [ "print(r.json())" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [], "source": [ "import csv\n", "f = open(\"pov.csv\", \"a\", newline=\"\")\n", "writer = csv.writer(f)\n", "writer.writerow(df)\n", "f.close()" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", " \n", " \n", " \n", " \n", " Median_household_income \n", " Under_18_in_poverty \n", " All_ages_in_Poverty \n", " Year \n", " State \n", " County \n", " \n", " \n", " \n", " \n", " 0 \n", " 50131 \n", " 3777 \n", " 9584 \n", " 2018 \n", " 48 \n", " 203 \n", " \n", " \n", "\n", " " ], "text/plain": [ " Median_household_income Under_18_in_poverty All_ages_in_Poverty Year \\\n", "0 50131 3777 9584 2018 \n", "\n", " State County \n", "0 48 203 " ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.to_csv(\"pov.csv\", index = False)\n", "pd.read_csv(\"pov.csv\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": {
Nov 07, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here