{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Session 4 Exercises" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import qeds\n", "\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Unemployment Data Questions" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
stateArizonaCaliforniaFloridaIllinoisMichiganNew YorkTexas
Date
2000-01-014.15.03.74.23.34.74.6
2000-02-014.15.03.74.23.24.74.6
2000-03-014.05.03.74.33.24.64.5
2000-04-014.05.13.74.33.34.64.4
2000-05-014.05.13.74.33.54.64.3
\n", "
" ], "text/plain": [ "state Arizona California Florida Illinois Michigan New York Texas\n", "Date \n", "2000-01-01 4.1 5.0 3.7 4.2 3.3 4.7 4.6\n", "2000-02-01 4.1 5.0 3.7 4.2 3.2 4.7 4.6\n", "2000-03-01 4.0 5.0 3.7 4.3 3.2 4.6 4.5\n", "2000-04-01 4.0 5.1 3.7 4.3 3.3 4.6 4.4\n", "2000-05-01 4.0 5.1 3.7 4.3 3.5 4.6 4.3" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## Load up the data -- this may take a couple seconds\n", "url = \"https://datascience.quantecon.org/assets/data/state_unemployment.csv\"\n", "unemp_raw = pd.read_csv(url, parse_dates=[\"Date\"])\n", "unemp_all = (\n", " unemp_raw\n", " .reset_index()\n", " .pivot_table(index=\"Date\", columns=\"state\", values=\"UnemploymentRate\")\n", ")\n", "\n", "# Will only work with this subset of data\n", "states = [\n", " \"Arizona\", \"California\", \"Florida\", \"Illinois\",\n", " \"Michigan\", \"New York\", \"Texas\"\n", "]\n", "unemp = unemp_all[states]\n", "unemp.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise**\n", "\n", "Imagine that we want to determine whether unemployment was high (> 6.5),\n", "medium (4.5 < x <= 6.5), or low (<= 4.5) for each state and each month.\n", "\n", "1. Write a Python function that takes a single number as an input and\n", " outputs a single string noting if that number is high, medium, or low. \n", "1. Pass your function to `applymap` (quiz: why `applymap` and not\n", " `agg` or `apply`?) and save the result in a new DataFrame called\n", " `unemp_bins`. \n", "1. (Challenging) This exercise has multiple parts: \n", " 1. Use another transform on `unemp_bins` to count how many\n", " times each state had each of the three classifications. \n", " - Hint 1: Will this value counting function be a Series or scalar\n", " transform? \n", " - Hint 2: Try googling \"pandas count unique value\" or something\n", " similar to find the right transform. \n", " 1. Construct a horizontal bar chart of the number of occurrences of\n", " each level with one bar per state and classification (21 total\n", " bars). \n", "1. (Challenging) Repeat the previous step, but count how many states had\n", " each classification in each month. Which month had the most states\n", " with high unemployment? What about medium and low? " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Part 1: Write a Python function to classify unemployment levels.\n", "def unemployment_classifier(ur):\n", " if ur > 6.5:\n", " return \"high\"\n", " elif ur > 4.5:\n", " return \"medium\"\n", " else:\n", " return \"low\"" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'medium'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unemployment_classifier(4.8)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# Part 2: Pass your function from part 1 to applymap\n", "unemp_bins = unemp.applymap(unemployment_classifier)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
stateArizonaCaliforniaFloridaIllinoisMichiganNew YorkTexas
Date
2000-01-01lowmediumlowlowlowmediummedium
2000-02-01lowmediumlowlowlowmediummedium
2000-03-01lowmediumlowlowlowmediumlow
2000-04-01lowmediumlowlowlowmediumlow
2000-05-01lowmediumlowlowlowmediumlow
........................
2017-08-01mediummediumlowmediummediummediumlow
2017-09-01mediumlowlowmediummediummediumlow
2017-10-01mediumlowlowmediummediummediumlow
2017-11-01mediumlowlowmediummediummediumlow
2017-12-01mediumlowlowmediummediummediumlow
\n", "

216 rows × 7 columns

\n", "
" ], "text/plain": [ "state Arizona California Florida Illinois Michigan New York Texas\n", "Date \n", "2000-01-01 low medium low low low medium medium\n", "2000-02-01 low medium low low low medium medium\n", "2000-03-01 low medium low low low medium low\n", "2000-04-01 low medium low low low medium low\n", "2000-05-01 low medium low low low medium low\n", "... ... ... ... ... ... ... ...\n", "2017-08-01 medium medium low medium medium medium low\n", "2017-09-01 medium low low medium medium medium low\n", "2017-10-01 medium low low medium medium medium low\n", "2017-11-01 medium low low medium medium medium low\n", "2017-12-01 medium low low medium medium medium low\n", "\n", "[216 rows x 7 columns]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unemp_bins" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
stateArizonaCaliforniaFloridaIllinoisMichiganNew YorkTexas
high7510668911426551
low4446919172258
medium971067910657129107
\n", "
" ], "text/plain": [ "state Arizona California Florida Illinois Michigan New York Texas\n", "high 75 106 68 91 142 65 51\n", "low 44 4 69 19 17 22 58\n", "medium 97 106 79 106 57 129 107" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unemp_bin_counts = unemp_bins.apply(pd.Series.value_counts, axis=0)\n", "unemp_bin_counts" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAEXCAYAAABWNASkAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/d3fzzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAm0klEQVR4nO3de3hU5dX38e8iIkdFTiIYa4IPCIRAIhEVOUoFVIqCtYJVgraiFh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knunkQooV/k7juA2UBfM6tiZslAE2BpVCMWidC2bduoXr06N998M/fddx8rVqzgjDPO4OuvvwZg37591KhRg1q1arFz507efvvt/H0LbnfppZfy/vvv8/HHHwNw4MAB/v3vf1f8BYkUocSeu5lNBToD9cxsKzDS3ScVta27rzGzV4G1QB4wSDNl5GSzevVqfvOb31CpUiUqV67Ms88+y5IlS7jqqqto2LAh8+fPJz09nZSUFBo3bszll1+ev+/AgQMLbffSSy/Rr18/vvvuOwAefvhhmjZtGqtLE8ln7rEfEcnIyPB4eliHpkKGrFu3jubNm8c6jMDQ9zP+mNlyd88oqk13qIqIBJCSu4hIACm5i4gEkJK7iEgAKbmLiASQkruISADpMXty0kga/veoHm/TY9eUaruZM2fSp08f1q1bR7NmzYrcpl27dnzwwQfRDE+kXKnnLnFv6tSptG/fnmnTph3Tdvhw6B48JXY51Si5S1zbv38/77//PpMmTcpP7gsWLKBLly7cdNNNpKamAlCzZk0AHnzwQdLS0khLS+Pcc8/l1ltvBWDs2LG0bNmSli1bMm7cOAA2bdpE8+bNuf3220lJSaFbt258++23AEycOJGLL76Y1q1bc/311+sBIBJ1Su4S12bNmkWPHj1o2rQpderUYcWKFQAsXbqURx555JhqkKNHjyY7O5v33nuPunXrcvfdd7N8+XJefPFF/vWvf/Hhhx8yceJEPvroIwA2btzIoEGDWLNmDWeddRavvfYaAH369GHZsmWsXLmS5s2bM2lSkRU9RMpMyV3i2tSpU+nbty8Affv2za/w2LZtW5KTk4vcx935+c9/ztChQ2nTpg2LFy+md+/e1KhRg5o1a9KnTx8WLVoEQHJyMmlpaQC0adOGTZs2AZCTk0OHDh1ITU1lypQprFmzpnwvVOKOPlCVuLVnzx7mzZtHTk4OZsbhw4cxM66++mpq1Khx3P1GjRpFYmJi/pBMcfWZqlSpkv8+ISEhf1hmwIABzJo1i9atW/PSSy+xYMGC6FyUROTJG3sW237v9LcqKJLIqecucWvGjBn079+fzZs3s2nTJj777DOSk5NZvHjxcfd56623mDNnDuPHj89f17FjR2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"output_type": "display_data" } ], "source": [ "# Part 3: Count the number of times each state had each classification.\n", "unemp_bin_counts.plot(kind=\"bar\")" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
highlowmedium
Date
2000-01-010.04.03.0
2000-02-010.04.03.0
2000-03-010.05.02.0
2000-04-010.05.02.0
2000-05-010.05.02.0
............
2017-08-010.02.05.0
2017-09-010.03.04.0
2017-10-010.03.04.0
2017-11-010.03.04.0
2017-12-010.03.04.0
\n", "

216 rows × 3 columns

\n", "
" ], "text/plain": [ " high low medium\n", "Date \n", "2000-01-01 0.0 4.0 3.0\n", "2000-02-01 0.0 4.0 3.0\n", "2000-03-01 0.0 5.0 2.0\n", "2000-04-01 0.0 5.0 2.0\n", "2000-05-01 0.0 5.0 2.0\n", "... ... ... ...\n", "2017-08-01 0.0 2.0 5.0\n", "2017-09-01 0.0 3.0 4.0\n", "2017-10-01 0.0 3.0 4.0\n", "2017-11-01 0.0 3.0 4.0\n", "2017-12-01 0.0 3.0 4.0\n", "\n", "[216 rows x 3 columns]" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Part 4: Apply the same transform from part 4, but to each date instead of to each state.\n", "unemp_bin_counts_month = unemp_bins.apply(\n", " pd.Series.value_counts, axis=\"columns\"\n", ").fillna(0)\n", "\n", "unemp_bin_counts_month" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Which month had the most states with high unemployment? What about medium and low?" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2009-04-01 00:00:00\n", "2001-09-01 00:00:00\n", "2000-08-01 00:00:00\n" ] } ], "source": [ "print(unemp_bin_counts_month[\"high\"].idxmax())\n", "print(unemp_bin_counts_month[\"medium\"].idxmax())\n", "print(unemp_bin_counts_month[\"low\"].idxmax())" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "high 2009-04-01\n", "low 2000-08-01\n", "medium 2001-09-01\n", "dtype: datetime64[ns]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unemp_bin_counts_month.agg(pd.Series.idxmax, axis=\"index\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python style conventions (including naming variables): https://www.python.org/dev/peps/pep-0008" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise**\n", "\n", "- For a single state of your choice, determine what the mean\n", " unemployment is during \"Low\", \"Medium\", and \"High\" unemployment times. \n", " - Think about how you would do this for all the\n", " states in our sample and write your thoughts... We will soon\n", " learn tools that will *greatly* simplify operations like\n", " this that operate on distinct *groups* of data at a time. \n", "- Which states in our sample performs the best during \"bad times?\" To\n", " determine this, compute the mean unemployment for each state only for\n", " months in which the mean unemployment rate in our sample is greater\n", " than 7. " ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.0636363636363635" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unemp.loc[unemp_bins.loc[:, \"Arizona\"] == 'low', \"Arizona\"].mean()" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.436082474226807" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unemp.loc[unemp_bins.loc[:, \"Arizona\"] == 'medium', \"Arizona\"].mean()" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8.73333333333333" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unemp.loc[unemp_bins.loc[:, \"Arizona\"] == 'high', \"Arizona\"].mean()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### WDI Data Questions" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "url = \"https://datascience.quantecon.org/assets/data/wdi_data.csv\"\n", "df = pd.read_csv(url)\n", "\n", "wdi = df.set_index([\"country\", \"year\"])\n", "wdiT = wdi.T" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
GovExpendConsumptionExportsImportsGDP
countryyear
Canada20170.3726651.0954750.5828310.6000311.868164
20160.3648991.0584260.5763940.5757751.814016
20150.3583031.0352080.5688590.5757931.794270
20140.3534851.0119880.5503230.5723441.782252
20130.3515410.9864000.5180400.5586361.732714
\n", "
" ], "text/plain": [ " GovExpend Consumption Exports Imports GDP\n", "country year \n", "Canada 2017 0.372665 1.095475 0.582831 0.600031 1.868164\n", " 2016 0.364899 1.058426 0.576394 0.575775 1.814016\n", " 2015 0.358303 1.035208 0.568859 0.575793 1.794270\n", " 2014 0.353485 1.011988 0.550323 0.572344 1.782252\n", " 2013 0.351541 0.986400 0.518040 0.558636 1.732714" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wdi.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise**\n", "\n", "Try setting `my_df` to some subset of the rows in `wdi` (use one of the\n", "`.loc` variations above).\n", "\n", "Then see what happens when you do `wdi / my_df` or `my_df ** wdi`.\n", "\n", "Try changing the subset of rows in `my_df` and repeat until you\n", "understand what is happening." ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
GovExpendConsumptionExportsImportsGDP
countryyear
Canada20100.3473320.9219520.4699490.5003411.613543
20150.3583031.0352080.5688590.5757931.794270
20170.3726651.0954750.5828310.6000311.868164
Germany20100.6533861.9154811.4437351.2661263.417095
20150.7061152.0336661.8030811.5270743.718482
\n", "
" ], "text/plain": [ " GovExpend Consumption Exports Imports GDP\n", "country year \n", "Canada 2010 0.347332 0.921952 0.469949 0.500341 1.613543\n", " 2015 0.358303 1.035208 0.568859 0.575793 1.794270\n", " 2017 0.372665 1.095475 0.582831 0.600031 1.868164\n", "Germany 2010 0.653386 1.915481 1.443735 1.266126 3.417095\n", " 2015 0.706115 2.033666 1.803081 1.527074 3.718482" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# my_df = wdi.loc[pd.IndexSlice[[\"Canada\", \"Germany\"], [2010, 2015, 2017]], :]\n", "# Produce the same output\n", "my_df = wdi.loc[([\"Canada\", \"Germany\"], [2010, 2015, 2017]), :]\n", "\n", "my_df.head()" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "foo = wdi / my_df" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "bar = wdi.divide(my_df)" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
GovExpendConsumptionExportsImportsGDP
countryyear
Canada20170.3726651.0954750.5828310.6000311.868164
20160.3648991.0584260.5763940.5757751.814016
20150.3583031.0352080.5688590.5757931.794270
20140.3534851.0119880.5503230.5723441.782252
20130.3515410.9864000.5180400.5586361.732714
\n", "
" ], "text/plain": [ " GovExpend Consumption Exports Imports GDP\n", "country year \n", "Canada 2017 0.372665 1.095475 0.582831 0.600031 1.868164\n", " 2016 0.364899 1.058426 0.576394 0.575775 1.814016\n", " 2015 0.358303 1.035208 0.568859 0.575793 1.794270\n", " 2014 0.353485 1.011988 0.550323 0.572344 1.782252\n", " 2013 0.351541 0.986400 0.518040 0.558636 1.732714" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wdi.head()" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
GovExpendConsumptionExportsImportsGDP
countryyear
Canada20100.3473320.9219520.4699490.5003411.613543
20150.3583031.0352080.5688590.5757931.794270
20170.3726651.0954750.5828310.6000311.868164
Germany20100.6533861.9154811.4437351.2661263.417095
20150.7061152.0336661.8030811.5270743.718482
\n", "
" ], "text/plain": [ " GovExpend Consumption Exports Imports GDP\n", "country year \n", "Canada 2010 0.347332 0.921952 0.469949 0.500341 1.613543\n", " 2015 0.358303 1.035208 0.568859 0.575793 1.794270\n", " 2017 0.372665 1.095475 0.582831 0.600031 1.868164\n", "Germany 2010 0.653386 1.915481 1.443735 1.266126 3.417095\n", " 2015 0.706115 2.033666 1.803081 1.527074 3.718482" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_df.head()" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
GovExpendConsumptionExportsImportsGDP
countryyear
Canada20000.2705530.6777130.4995260.3808231.342805
20010.2797670.6942300.4846960.3620231.366590
20020.2860940.7219740.4904650.3686151.407725
20030.2943350.7417960.4819930.3841991.433089
20040.2998540.7643570.5086570.4167541.477317
.....................
United States20132.35338110.6872142.1186392.60019815.853796
20142.33407111.0006192.2095552.73222816.242526
20152.37313011.4098002.2222282.88133716.710459
20162.40798111.7221332.2199372.93600416.972348
20172.40574312.0192662.2870713.06995417.348627
\n", "

72 rows × 5 columns

\n", "
" ], "text/plain": [ " GovExpend Consumption Exports Imports GDP\n", "country year \n", "Canada 2000 0.270553 0.677713 0.499526 0.380823 1.342805\n", " 2001 0.279767 0.694230 0.484696 0.362023 1.366590\n", " 2002 0.286094 0.721974 0.490465 0.368615 1.407725\n", " 2003 0.294335 0.741796 0.481993 0.384199 1.433089\n", " 2004 0.299854 0.764357 0.508657 0.416754 1.477317\n", "... ... ... ... ... ...\n", "United States 2013 2.353381 10.687214 2.118639 2.600198 15.853796\n", " 2014 2.334071 11.000619 2.209555 2.732228 16.242526\n", " 2015 2.373130 11.409800 2.222228 2.881337 16.710459\n", " 2016 2.407981 11.722133 2.219937 2.936004 16.972348\n", " 2017 2.405743 12.019266 2.287071 3.069954 17.348627\n", "\n", "[72 rows x 5 columns]" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wdi.divide(my_df, fill_value=1)" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
GovExpendConsumptionExportsImportsGDP
countryyear
Canada20000.00.00.00.00.0
20010.00.00.00.00.0
20020.00.00.00.00.0
20030.00.00.00.00.0
20040.00.00.00.00.0
.....................
United States20130.00.00.00.00.0
20140.00.00.00.00.0
20150.00.00.00.00.0
20160.00.00.00.00.0
20170.00.00.00.00.0
\n", "

72 rows × 5 columns

\n", "
" ], "text/plain": [ " GovExpend Consumption Exports Imports GDP\n", "country year \n", "Canada 2000 0.0 0.0 0.0 0.0 0.0\n", " 2001 0.0 0.0 0.0 0.0 0.0\n", " 2002 0.0 0.0 0.0 0.0 0.0\n", " 2003 0.0 0.0 0.0 0.0 0.0\n", " 2004 0.0 0.0 0.0 0.0 0.0\n", "... ... ... ... ... ...\n", "United States 2013 0.0 0.0 0.0 0.0 0.0\n", " 2014 0.0 0.0 0.0 0.0 0.0\n", " 2015 0.0 0.0 0.0 0.0 0.0\n", " 2016 0.0 0.0 0.0 0.0 0.0\n", " 2017 0.0 0.0 0.0 0.0 0.0\n", "\n", "[72 rows x 5 columns]" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(wdi / my_df).fillna(0.0) # Fills with just 0s" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
GovExpendConsumptionExportsImportsGDP
countryyear
Canada20000.01.02.03.04.0
20010.01.02.03.04.0
20020.01.02.03.04.0
20030.01.02.03.04.0
20040.01.02.03.04.0
.....................
United States20130.01.02.03.04.0
20140.01.02.03.04.0
20150.01.02.03.04.0
20160.01.02.03.04.0
20170.01.02.03.04.0
\n", "

72 rows × 5 columns

\n", "
" ], "text/plain": [ " GovExpend Consumption Exports Imports GDP\n", "country year \n", "Canada 2000 0.0 1.0 2.0 3.0 4.0\n", " 2001 0.0 1.0 2.0 3.0 4.0\n", " 2002 0.0 1.0 2.0 3.0 4.0\n", " 2003 0.0 1.0 2.0 3.0 4.0\n", " 2004 0.0 1.0 2.0 3.0 4.0\n", "... ... ... ... ... ...\n", "United States 2013 0.0 1.0 2.0 3.0 4.0\n", " 2014 0.0 1.0 2.0 3.0 4.0\n", " 2015 0.0 1.0 2.0 3.0 4.0\n", " 2016 0.0 1.0 2.0 3.0 4.0\n", " 2017 0.0 1.0 2.0 3.0 4.0\n", "\n", "[72 rows x 5 columns]" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fill_vals = {\n", " \"GovExpend\": 0,\n", " \"Consumption\": 1,\n", " \"Exports\": 2,\n", " \"Imports\": 3,\n", " \"GDP\": 4\n", "}\n", "\n", "# Fills with value from associated dictionary\n", "(wdi / my_df).fillna(fill_vals)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "foo.divide?" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
GovExpendConsumptionExportsImportsGDP
countryyear
Canada20101.01.01.01.01.0
20151.01.01.01.01.0
20171.01.01.01.01.0
Germany20101.01.01.01.01.0
20151.01.01.01.01.0
20171.01.01.01.01.0
\n", "
" ], "text/plain": [ " GovExpend Consumption Exports Imports GDP\n", "country year \n", "Canada 2010 1.0 1.0 1.0 1.0 1.0\n", " 2015 1.0 1.0 1.0 1.0 1.0\n", " 2017 1.0 1.0 1.0 1.0 1.0\n", "Germany 2010 1.0 1.0 1.0 1.0 1.0\n", " 2015 1.0 1.0 1.0 1.0 1.0\n", " 2017 1.0 1.0 1.0 1.0 1.0" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "foo.loc[my_df.index, :]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise**\n", "\n", "Use `pd.IndexSlice` to extract all data from `wdiT` where the `year`\n", "level of the column names (the second level) is one of 2010, 2012, and 2014\n" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
countryCanadaGermanyUnited KingdomUnited States
year201420122010201420122010201420122010201420122010
GovExpend0.3534850.3543420.3473320.6859900.6664540.6533860.5388880.5281940.5211462.3340712.3988732.510143
Consumption1.0119880.9612260.9219521.9999531.9673901.9154811.6757161.6125501.59856311.00061910.53404210.185836
Exports0.5503230.5059690.4699491.7122701.6074551.4437350.7740220.7454840.6908242.2095552.0455091.846280
Imports0.5723440.5477560.5003411.4454091.3541221.2661260.8273110.7726920.7450652.7322282.5606772.360183
GDP1.7822521.6934281.6135433.6549243.5595873.4170952.6571592.5293232.45290016.24252615.56703814.992053
\n", "
" ], "text/plain": [ "country Canada Germany \\\n", "year 2014 2012 2010 2014 2012 2010 \n", "GovExpend 0.353485 0.354342 0.347332 0.685990 0.666454 0.653386 \n", "Consumption 1.011988 0.961226 0.921952 1.999953 1.967390 1.915481 \n", "Exports 0.550323 0.505969 0.469949 1.712270 1.607455 1.443735 \n", "Imports 0.572344 0.547756 0.500341 1.445409 1.354122 1.266126 \n", "GDP 1.782252 1.693428 1.613543 3.654924 3.559587 3.417095 \n", "\n", "country United Kingdom United States \\\n", "year 2014 2012 2010 2014 2012 \n", "GovExpend 0.538888 0.528194 0.521146 2.334071 2.398873 \n", "Consumption 1.675716 1.612550 1.598563 11.000619 10.534042 \n", "Exports 0.774022 0.745484 0.690824 2.209555 2.045509 \n", "Imports 0.827311 0.772692 0.745065 2.732228 2.560677 \n", "GDP 2.657159 2.529323 2.452900 16.242526 15.567038 \n", "\n", "country \n", "year 2010 \n", "GovExpend 2.510143 \n", "Consumption 10.185836 \n", "Exports 1.846280 \n", "Imports 2.360183 \n", "GDP 14.992053 " ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wdiT.loc[:, pd.IndexSlice[:, [2010, 2012, 2014]]]" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
countryCanadaGermanyUnited KingdomUnited States
year201420122010201420122010201420122010201420122010
GovExpend0.3534850.3543420.3473320.6859900.6664540.6533860.5388880.5281940.5211462.3340712.3988732.510143
Consumption1.0119880.9612260.9219521.9999531.9673901.9154811.6757161.6125501.59856311.00061910.53404210.185836
Exports0.5503230.5059690.4699491.7122701.6074551.4437350.7740220.7454840.6908242.2095552.0455091.846280
Imports0.5723440.5477560.5003411.4454091.3541221.2661260.8273110.7726920.7450652.7322282.5606772.360183
GDP1.7822521.6934281.6135433.6549243.5595873.4170952.6571592.5293232.45290016.24252615.56703814.992053
\n", "
" ], "text/plain": [ "country Canada Germany \\\n", "year 2014 2012 2010 2014 2012 2010 \n", "GovExpend 0.353485 0.354342 0.347332 0.685990 0.666454 0.653386 \n", "Consumption 1.011988 0.961226 0.921952 1.999953 1.967390 1.915481 \n", "Exports 0.550323 0.505969 0.469949 1.712270 1.607455 1.443735 \n", "Imports 0.572344 0.547756 0.500341 1.445409 1.354122 1.266126 \n", "GDP 1.782252 1.693428 1.613543 3.654924 3.559587 3.417095 \n", "\n", "country United Kingdom United States \\\n", "year 2014 2012 2010 2014 2012 \n", "GovExpend 0.538888 0.528194 0.521146 2.334071 2.398873 \n", "Consumption 1.675716 1.612550 1.598563 11.000619 10.534042 \n", "Exports 0.774022 0.745484 0.690824 2.209555 2.045509 \n", "Imports 0.827311 0.772692 0.745065 2.732228 2.560677 \n", "GDP 2.657159 2.529323 2.452900 16.242526 15.567038 \n", "\n", "country \n", "year 2010 \n", "GovExpend 2.510143 \n", "Consumption 10.185836 \n", "Exports 1.846280 \n", "Imports 2.360183 \n", "GDP 14.992053 " ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "idx = pd.IndexSlice[:, [2010, 2012, 2014]]\n", "wdiT.loc[:, idx]" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
GovExpendConsumptionExportsImportsGDP
countryyear
Canada20170.3726651.0954750.5828310.6000311.868164
20160.3648991.0584260.5763940.5757751.814016
20150.3583031.0352080.5688590.5757931.794270
20140.3534851.0119880.5503230.5723441.782252
20130.3515410.9864000.5180400.5586361.732714
.....................
United States20042.2679999.3114311.3359782.10858513.846058
20032.2335198.9747081.2181991.89282513.339312
20022.1931888.6983061.1921801.80410512.968263
20012.1120388.4804611.2132531.74079712.746262
20002.0405008.2720971.2877391.79099512.620268
\n", "

72 rows × 5 columns

\n", "
" ], "text/plain": [ " GovExpend Consumption Exports Imports GDP\n", "country year \n", "Canada 2017 0.372665 1.095475 0.582831 0.600031 1.868164\n", " 2016 0.364899 1.058426 0.576394 0.575775 1.814016\n", " 2015 0.358303 1.035208 0.568859 0.575793 1.794270\n", " 2014 0.353485 1.011988 0.550323 0.572344 1.782252\n", " 2013 0.351541 0.986400 0.518040 0.558636 1.732714\n", "... ... ... ... ... ...\n", "United States 2004 2.267999 9.311431 1.335978 2.108585 13.846058\n", " 2003 2.233519 8.974708 1.218199 1.892825 13.339312\n", " 2002 2.193188 8.698306 1.192180 1.804105 12.968263\n", " 2001 2.112038 8.480461 1.213253 1.740797 12.746262\n", " 2000 2.040500 8.272097 1.287739 1.790995 12.620268\n", "\n", "[72 rows x 5 columns]" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wdi" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
countryyearvariablevalue
0Canada2017GovExpend0.372665
1Canada2016GovExpend0.364899
2Canada2015GovExpend0.358303
3Canada2014GovExpend0.353485
4Canada2013GovExpend0.351541
...............
355United States2004GDP13.846058
356United States2003GDP13.339312
357United States2002GDP12.968263
358United States2001GDP12.746262
359United States2000GDP12.620268
\n", "

360 rows × 4 columns

\n", "
" ], "text/plain": [ " country year variable value\n", "0 Canada 2017 GovExpend 0.372665\n", "1 Canada 2016 GovExpend 0.364899\n", "2 Canada 2015 GovExpend 0.358303\n", "3 Canada 2014 GovExpend 0.353485\n", "4 Canada 2013 GovExpend 0.351541\n", ".. ... ... ... ...\n", "355 United States 2004 GDP 13.846058\n", "356 United States 2003 GDP 13.339312\n", "357 United States 2002 GDP 12.968263\n", "358 United States 2001 GDP 12.746262\n", "359 United States 2000 GDP 12.620268\n", "\n", "[360 rows x 4 columns]" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wdi_melted = wdi.reset_index().melt(id_vars=[\"country\", \"year\"])\n", "wdi_melted" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
variableConsumptionExportsGDPGovExpendImports
countryyear
Canada20000.6777130.4995261.3428050.2705530.380823
20010.6942300.4846961.3665900.2797670.362023
20020.7219740.4904651.4077250.2860940.368615
20030.7417960.4819931.4330890.2943350.384199
20040.7643570.5086571.4773170.2998540.416754
.....................
United States201310.6872142.11863915.8537962.3533812.600198
201411.0006192.20955516.2425262.3340712.732228
201511.4098002.22222816.7104592.3731302.881337
201611.7221332.21993716.9723482.4079812.936004
201712.0192662.28707117.3486272.4057433.069954
\n", "

72 rows × 5 columns

\n", "
" ], "text/plain": [ "variable Consumption Exports GDP GovExpend Imports\n", "country year \n", "Canada 2000 0.677713 0.499526 1.342805 0.270553 0.380823\n", " 2001 0.694230 0.484696 1.366590 0.279767 0.362023\n", " 2002 0.721974 0.490465 1.407725 0.286094 0.368615\n", " 2003 0.741796 0.481993 1.433089 0.294335 0.384199\n", " 2004 0.764357 0.508657 1.477317 0.299854 0.416754\n", "... ... ... ... ... ...\n", "United States 2013 10.687214 2.118639 15.853796 2.353381 2.600198\n", " 2014 11.000619 2.209555 16.242526 2.334071 2.732228\n", " 2015 11.409800 2.222228 16.710459 2.373130 2.881337\n", " 2016 11.722133 2.219937 16.972348 2.407981 2.936004\n", " 2017 12.019266 2.287071 17.348627 2.405743 3.069954\n", "\n", "[72 rows x 5 columns]" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wdi_melted.pivot(index=[\"country\", \"year\"], columns=\"variable\", values=\"value\")" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [], "source": [ "wdi_melted.pivot_table?" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.3" } }, "nbformat": 4, "nbformat_minor": 5 }