diff --git a/notebooks/Block_0/Exercise Sheet - Basics Numpy.ipynb b/notebooks/Block_0/Exercise Sheet - Basics Numpy.ipynb index c4bd3e90cc64459f8705b6d7ec6c38dafb180682..1448f33090fe73ef340591659ef141bcfa3809f3 100644 --- a/notebooks/Block_0/Exercise Sheet - Basics Numpy.ipynb +++ b/notebooks/Block_0/Exercise Sheet - Basics Numpy.ipynb @@ -156,21 +156,138 @@ "metadata": {}, "source": [ "In this problem you will have to make use of **Pandas** to load and analyse a small, fictional database weather.csv (the same as used in the Script).\n", - "The data containes the temperature in 4 cities over 6 months. Download the dataset and save it somewhere practically, possibly in the same folder as this notebook." + "The data containes the temperature in 4 cities over 6 months. Download the dataset and save it somewhere practical, possibly in the same folder as this notebook." ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Collecting pandas\n", + " Downloading pandas-1.4.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.7 MB)\n", + "\u001b[2K \u001b[90mâ”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”\u001b[0m \u001b[32m11.7/11.7 MB\u001b[0m \u001b[31m26.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m0:01\u001b[0m\n", + "\u001b[?25hRequirement already satisfied: numpy>=1.18.5 in /opt/conda/lib/python3.9/site-packages (from pandas) (1.22.3)\n", + "Requirement already satisfied: pytz>=2020.1 in /opt/conda/lib/python3.9/site-packages (from pandas) (2021.3)\n", + "Requirement already satisfied: python-dateutil>=2.8.1 in /opt/conda/lib/python3.9/site-packages (from pandas) (2.8.2)\n", + "Requirement already satisfied: six>=1.5 in /opt/conda/lib/python3.9/site-packages (from python-dateutil>=2.8.1->pandas) (1.16.0)\n", + "Installing collected packages: pandas\n", + "Successfully installed pandas-1.4.1\n", + "\u001b[33mWARNING: You are using pip version 22.0.3; however, version 22.0.4 is available.\n", + "You should consider upgrading via the '/opt/conda/bin/python -m pip install --upgrade pip' command.\u001b[0m\u001b[33m\n", + "\u001b[0mNote: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "pip install pandas" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "<div>\n", + "<style scoped>\n", + " .dataframe tbody tr th:only-of-type {\n", + " vertical-align: middle;\n", + " }\n", + "\n", + " .dataframe tbody tr th {\n", + " vertical-align: top;\n", + " }\n", + "\n", + " .dataframe thead th {\n", + " text-align: right;\n", + " }\n", + "</style>\n", + "<table border=\"1\" class=\"dataframe\">\n", + " <thead>\n", + " <tr style=\"text-align: right;\">\n", + " <th></th>\n", + " <th>Luzern</th>\n", + " <th>Basel</th>\n", + " <th>Chur</th>\n", + " <th>Zurich</th>\n", + " </tr>\n", + " </thead>\n", + " <tbody>\n", + " <tr>\n", + " <th>Jan</th>\n", + " <td>2</td>\n", + " <td>5</td>\n", + " <td>-3</td>\n", + " <td>4</td>\n", + " </tr>\n", + " <tr>\n", + " <th>Feb</th>\n", + " <td>5</td>\n", + " <td>6</td>\n", + " <td>1</td>\n", + " <td>0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>Mar</th>\n", + " <td>10</td>\n", + " <td>11</td>\n", + " <td>13</td>\n", + " <td>8</td>\n", + " </tr>\n", + " <tr>\n", + " <th>Apr</th>\n", + " <td>16</td>\n", + " <td>12</td>\n", + " <td>14</td>\n", + " <td>17</td>\n", + " </tr>\n", + " <tr>\n", + " <th>May</th>\n", + " <td>21</td>\n", + " <td>23</td>\n", + " <td>21</td>\n", + " <td>20</td>\n", + " </tr>\n", + " <tr>\n", + " <th>Jun</th>\n", + " <td>25</td>\n", + " <td>21</td>\n", + " <td>23</td>\n", + " <td>27</td>\n", + " </tr>\n", + " </tbody>\n", + "</table>\n", + "</div>" + ], + "text/plain": [ + " Luzern Basel Chur Zurich\n", + "Jan 2 5 -3 4\n", + "Feb 5 6 1 0\n", + "Mar 10 11 13 8\n", + "Apr 16 12 14 17\n", + "May 21 23 21 20\n", + "Jun 25 21 23 27" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# First import Pandas\n", "import pandas as pd\n", "\n", "# load the database using pandas.read_csv. The file location is different for everyone. If you made a new folder in this folder named \"data\", the path would be ./data/weather.csv\n", "# Example: data = pd.read_csv(\"/data/weather.csv\")\n", - "data = None\n", + "data = pd.read_csv(\"./data/weather.csv\")\n", "\n", "data" ] @@ -193,7 +310,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 31, "metadata": {}, "outputs": [ { @@ -201,16 +318,22 @@ "output_type": "stream", "text": [ "Temperature in Basel is:\n", - " None \n", + " Jan 5\n", + "Feb 6\n", + "Mar 11\n", + "Apr 12\n", + "May 23\n", + "Jun 21\n", + "Name: Basel, dtype: int64 \n", "\n", "Temperature in Chur in February is:\n", - " None\n" + " 1\n" ] } ], "source": [ - "t_basel = None\n", - "t_chur_feb = None\n", + "t_basel = data.loc[:, \"Basel\"]\n", + "t_chur_feb = data.loc[\"Feb\", \"Chur\"]\n", "\n", "print(\"Temperature in Basel is:\\n\", t_basel, \"\\n\")\n", "print(\"Temperature in Chur in February is:\\n\", t_chur_feb)" @@ -234,23 +357,112 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "None\n" + "Zurich\n" ] + }, + { + "data": { + "text/html": [ + "<div>\n", + "<style scoped>\n", + " .dataframe tbody tr th:only-of-type {\n", + " vertical-align: middle;\n", + " }\n", + "\n", + " .dataframe tbody tr th {\n", + " vertical-align: top;\n", + " }\n", + "\n", + " .dataframe thead th {\n", + " text-align: right;\n", + " }\n", + "</style>\n", + "<table border=\"1\" class=\"dataframe\">\n", + " <thead>\n", + " <tr style=\"text-align: right;\">\n", + " <th></th>\n", + " <th>Luzern</th>\n", + " <th>Bern</th>\n", + " <th>Chur</th>\n", + " <th>Zurich</th>\n", + " </tr>\n", + " </thead>\n", + " <tbody>\n", + " <tr>\n", + " <th>Jan</th>\n", + " <td>2</td>\n", + " <td>5</td>\n", + " <td>-3</td>\n", + " <td>4</td>\n", + " </tr>\n", + " <tr>\n", + " <th>Feb</th>\n", + " <td>5</td>\n", + " <td>6</td>\n", + " <td>1</td>\n", + " <td>0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>Mar</th>\n", + " <td>10</td>\n", + " <td>11</td>\n", + " <td>13</td>\n", + " <td>8</td>\n", + " </tr>\n", + " <tr>\n", + " <th>Apr</th>\n", + " <td>16</td>\n", + " <td>12</td>\n", + " <td>14</td>\n", + " <td>17</td>\n", + " </tr>\n", + " <tr>\n", + " <th>May</th>\n", + " <td>21</td>\n", + " <td>23</td>\n", + " <td>21</td>\n", + " <td>20</td>\n", + " </tr>\n", + " <tr>\n", + " <th>Jun</th>\n", + " <td>25</td>\n", + " <td>21</td>\n", + " <td>23</td>\n", + " <td>27</td>\n", + " </tr>\n", + " </tbody>\n", + "</table>\n", + "</div>" + ], + "text/plain": [ + " Luzern Bern Chur Zurich\n", + "Jan 2 5 -3 4\n", + "Feb 5 6 1 0\n", + "Mar 10 11 13 8\n", + "Apr 16 12 14 17\n", + "May 21 23 21 20\n", + "Jun 25 21 23 27" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "# Find the name of column 3:\n", - "name_c3 = None\n", + "name_c3 = data.columns[3]\n", "print(name_c3)\n", " \n", "# Change \"Basel\" to \"Bern\"\n", + "data = data.rename(columns={\"Basel\": \"Bern\"})\n", "\n", "data" ] @@ -267,24 +479,24 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Average temperature in Chur was: None\n", - "Average temperature in Spring was: 0\n" + "Average temperature in Chur was: 11.5\n", + "Average temperature in Spring was: 15.5\n" ] } ], "source": [ - "t_chur_mean = None\n", + "t_chur_mean = data.loc[:, \"Chur\"].mean()\n", "\n", "\n", - "t_spring_mean = 0\n", - "# Hint: mean() on a DataFrame gives the result in a Serries. The axis of the function to be applied on can be set with axis={index (0), columns (1)}.\n", + "t_spring_mean = data.loc[\"Mar\":\"May\", :].mean().mean()\n", + "# Hint: mean() on a DataFrame gives the result in a Series. The axis of the function to be applied on can be set with axis={index (0), columns (1)}.\n", "# To find the mean of the whole matrix, the mean method can be applied twice\n", "\n", "\n", @@ -303,20 +515,201 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 41, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "<div>\n", + "<style scoped>\n", + " .dataframe tbody tr th:only-of-type {\n", + " vertical-align: middle;\n", + " }\n", + "\n", + " .dataframe tbody tr th {\n", + " vertical-align: top;\n", + " }\n", + "\n", + " .dataframe thead th {\n", + " text-align: right;\n", + " }\n", + "</style>\n", + "<table border=\"1\" class=\"dataframe\">\n", + " <thead>\n", + " <tr style=\"text-align: right;\">\n", + " <th></th>\n", + " <th>Luzern</th>\n", + " <th>Bern</th>\n", + " <th>Chur</th>\n", + " <th>Zurich</th>\n", + " </tr>\n", + " </thead>\n", + " <tbody>\n", + " <tr>\n", + " <th>Feb</th>\n", + " <td>5</td>\n", + " <td>6</td>\n", + " <td>1</td>\n", + " <td>0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>Jan</th>\n", + " <td>2</td>\n", + " <td>5</td>\n", + " <td>-3</td>\n", + " <td>4</td>\n", + " </tr>\n", + " <tr>\n", + " <th>Mar</th>\n", + " <td>10</td>\n", + " <td>11</td>\n", + " <td>13</td>\n", + " <td>8</td>\n", + " </tr>\n", + " <tr>\n", + " <th>Apr</th>\n", + " <td>16</td>\n", + " <td>12</td>\n", + " <td>14</td>\n", + " <td>17</td>\n", + " </tr>\n", + " <tr>\n", + " <th>May</th>\n", + " <td>21</td>\n", + " <td>23</td>\n", + " <td>21</td>\n", + " <td>20</td>\n", + " </tr>\n", + " <tr>\n", + " <th>Jun</th>\n", + " <td>25</td>\n", + " <td>21</td>\n", + " <td>23</td>\n", + " <td>27</td>\n", + " </tr>\n", + " </tbody>\n", + "</table>\n", + "</div>" + ], + "text/plain": [ + " Luzern Bern Chur Zurich\n", + "Feb 5 6 1 0\n", + "Jan 2 5 -3 4\n", + "Mar 10 11 13 8\n", + "Apr 16 12 14 17\n", + "May 21 23 21 20\n", + "Jun 25 21 23 27" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Sort data based on Zurich" + "# Sort data based on Zurich\n", + "data.sort_values(\"Zurich\")" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 43, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "<div>\n", + "<style scoped>\n", + " .dataframe tbody tr th:only-of-type {\n", + " vertical-align: middle;\n", + " }\n", + "\n", + " .dataframe tbody tr th {\n", + " vertical-align: top;\n", + " }\n", + "\n", + " .dataframe thead th {\n", + " text-align: right;\n", + " }\n", + "</style>\n", + "<table border=\"1\" class=\"dataframe\">\n", + " <thead>\n", + " <tr style=\"text-align: right;\">\n", + " <th></th>\n", + " <th>Luzern</th>\n", + " <th>Basel</th>\n", + " <th>Chur</th>\n", + " <th>Zurich</th>\n", + " </tr>\n", + " </thead>\n", + " <tbody>\n", + " <tr>\n", + " <th>May</th>\n", + " <td>21</td>\n", + " <td>23</td>\n", + " <td>21</td>\n", + " <td>20</td>\n", + " </tr>\n", + " <tr>\n", + " <th>Jun</th>\n", + " <td>25</td>\n", + " <td>21</td>\n", + " <td>23</td>\n", + " <td>27</td>\n", + " </tr>\n", + " <tr>\n", + " <th>Apr</th>\n", + " <td>16</td>\n", + " <td>12</td>\n", + " <td>14</td>\n", + " <td>17</td>\n", + " </tr>\n", + " <tr>\n", + " <th>Mar</th>\n", + " <td>10</td>\n", + " <td>11</td>\n", + " <td>13</td>\n", + " <td>8</td>\n", + " </tr>\n", + " <tr>\n", + " <th>Feb</th>\n", + " <td>5</td>\n", + " <td>6</td>\n", + " <td>1</td>\n", + " <td>0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>Jan</th>\n", + " <td>2</td>\n", + " <td>5</td>\n", + " <td>-3</td>\n", + " <td>4</td>\n", + " </tr>\n", + " </tbody>\n", + "</table>\n", + "</div>" + ], + "text/plain": [ + " Luzern Basel Chur Zurich\n", + "May 21 23 21 20\n", + "Jun 25 21 23 27\n", + "Apr 16 12 14 17\n", + "Mar 10 11 13 8\n", + "Feb 5 6 1 0\n", + "Jan 2 5 -3 4" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Sort data based on Basel, descending with temperature" + "# Sort data based on Basel, descending with temperature\n", + "data = data.rename(columns={\"Bern\": \"Basel\"})\n", + "data.sort_values(\"Basel\", ascending=False)" ] }, {