{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Do-It-Yourself" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This section is all about you taking charge of the steering wheel and choosing your own adventure. For this block, we are going to use what we've learnt [before](lab_B) to take a look at a dataset of casualties in the war in Afghanistan. The data was originally released by Wikileaks, and the version we will use is published by The Guardian.\n", "\n", "```{margin}\n", "You can read a bit more about the data at The Guardian's [data blog](http://www.theguardian.com/news/datablog/2010/jul/27/wikileaks-afghanistan-data-datajournalism)\n", "``` " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data preparation\n", "\n", "Before you can set off on your data journey, the dataset needs to be read, and there's a couple of details we will get out of the way so it is then easier for you to start working.\n", "\n", "The data are published on a Google Sheet you can check out at:\n", "\n", "> [https://docs.google.com/spreadsheets/d/1EAx8_ksSCmoWW_SlhFyq2QrRn0FNNhcg1TtDFJzZRgc/edit?hl=en#gid=1](https://docs.google.com/spreadsheets/d/1EAx8_ksSCmoWW_SlhFyq2QrRn0FNNhcg1TtDFJzZRgc/edit?hl=en#gid=1)\n", "\n", "As you will see, each row includes casualties recorded month by month, split by Taliban, Civilians, Afghan forces, and NATO.\n", "\n", "To read it into a Python session, we need to slightly modify the URL to access it into:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'https://docs.google.com/spreadsheets/d/1EAx8_ksSCmoWW_SlhFyq2QrRn0FNNhcg1TtDFJzZRgc/export?format=csv&gid=1'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "url = (\"https://docs.google.com/spreadsheets/d/\"\\\n", " \"1EAx8_ksSCmoWW_SlhFyq2QrRn0FNNhcg1TtDFJzZRgc/\"\\\n", " \"export?format=csv&gid=1\")\n", "url" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note how we split the url into three lines so it is more readable in narrow screens. The result however, stored in `url`, is the same as one long string.\n", "\n", "This allows us to read the data straight into a DataFrame, as we have done in the [previous session](lab_B):" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "db = pandas.read_csv(url, skiprows=[0, -1], thousands=\",\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note also we use the `skiprows=[0, -1]` to avoid reading the top (`0`) and bottom (`-1`) rows which, if you check on the Google Sheet, involves the title of the table.\n", "\n", "Now we are good to go!" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | Year | \n", "Month | \n", "Taliban | \n", "Civilians | \n", "Afghan forces | \n", "Nato (detailed in spreadsheet) | \n", "Nato - official figures | \n", "
---|---|---|---|---|---|---|---|
0 | \n", "2004.0 | \n", "January | \n", "15 | \n", "51 | \n", "23 | \n", "NaN | \n", "11.0 | \n", "
1 | \n", "2004.0 | \n", "February | \n", "NaN | \n", "7 | \n", "4 | \n", "5 | \n", "2.0 | \n", "
2 | \n", "2004.0 | \n", "March | \n", "19 | \n", "2 | \n", "NaN | \n", "2 | \n", "3.0 | \n", "
3 | \n", "2004.0 | \n", "April | \n", "5 | \n", "3 | \n", "19 | \n", "NaN | \n", "3.0 | \n", "
4 | \n", "2004.0 | \n", "May | \n", "18 | \n", "29 | \n", "56 | \n", "6 | \n", "9.0 | \n", "