add comments to self defined functions
"pip install lxml\n",
"pip install tabula-py```\n",
"\n",
- "<a href=\"https://pypi.org/project/tabula-py/\">`tabula-py`</a> can read table of PDF and convert into panda's data frame. In order to have tabula-py work on your environment, please make sure you have Java 7 or 8 on your machinebefore installing <a href=\"https://github.com/chezou/tabula-py\">tabula-py</a>. "
+ "<a href=\"https://pypi.org/project/tabula-py/\">`tabula-py`</a> can read table of PDF and convert into panda's `DataFrame`. In order to have tabula-py work on your environment, please make sure you have Java 7 or 8 on your machine before installing <a href=\"https://github.com/chezou/tabula-py\">tabula-py</a>. "
]
},
{
"source": [
"## Step 2. Read table from PDF\n",
"\n",
- "**California Department of Forestry & Fire Protection (CAL FIRE)** provides statistics and reports that are tracked on a weekly basis and also snapshots of the number of fires and acres burned. These are preliminary numbers taken from the Computer Aided Dispatch (CAD) system, and will likely change as dispatched wildfires may end up being other types of fires or false alarms <a href=\"#References\"> [1]</a>.\n",
+ "**California Department of Forestry & Fire Protection (CAL FIRE)** provides statistics and reports that are tracked on a weekly basis and also snapshots of the number of fires and acres burned. These are preliminary numbers taken from the Computer Aided Dispatch system, and will likely change as dispatched wildfires may end up being other types of fires or false alarms <a href=\"#References\"> [1]</a>.\n",
"\n",
"The table of **Number of Fires and Acres** can be fetched from the web page and parsed as a Pandas data frame (as following):"
]
"source": [
"import re\n",
"\n",
+ "\"\"\"Used to extract numbers from composite string e.g. \"271,911 CA /\\r43,666 NV\" \n",
+ " and add the acres within two or more states\n",
+ "\"\"\"\n",
"def extract_and_sum(s):\n",
" tmp = map(int, re.sub(r'[ a-zA-Z,]+[ a-zA-Z,]+', '', s, re.I).replace(',','').split('/\\r'))\n",
" return sum(list(tmp))\n",
"\n",
+ "\"\"\"Used to turn numerics e.g. 273,911 to int without delimitors e.g. 273911\n",
+ "\"\"\"\n",
"def replace_and_reassign(s):\n",
" return int(s.replace(',',''))\n",
"\n",
},
"outputs": [],
"source": [
+ "\"\"\"Using `numpy.arrange` in creating new rows as place holder to hold the splitted strings from df[col] based on `sep`;\n",
+ " then `iloc` and `assign` to place the splitted results into newly added rows\n",
+ "\"\"\"\n",
"def explode_str(df, col, sep):\n",
" s = df[col]\n",
" i = np.arange(len(s)).repeat(s.str.count(sep) + 1)\n",
"res[\"url\"]"
]
},
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "The `res[\"url\"]` shown above points to the URL address of the exported map which displays the top 20 most impacted counties in California by wildfire. If interested in creating more fun maps from the PDF files provided by **CAL FIRE**, you can repeat the previously done workflow with two other links stored in variable `pdf_url_list`."
+ ]
+ },
{
"cell_type": "markdown",
"metadata": {},
