{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "2759538c-8e99-4c11-b770-7f777480d887",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from datetime import date, timedelta\n",
    "import requests\n",
    "plt.style.use('https://github.com/dhaitz/matplotlib-stylesheets/raw/master/pitayasmoothie-dark.mplstyle')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff78c6ed-5348-4f1e-89b4-75a4e77298ce",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "# Application Programming Interfaces"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dab62750-7a06-453a-bff6-33704654eb0e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Task 1\n",
    "\n",
    "wake a response to the Mensa-Api for the timeframe today + 1 Days and today + 7 Days and extract the current Data for meals. Save your data in the Variable meals."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af237c00-26f6-481e-9269-39770f9c6166",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "source": [
    "Get `today`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bd6a280c-5acc-4881-a98c-4518375d47e5",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2023, 12, 22)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "today = date.today()\n",
    "today"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d01efe7-c776-4299-a936-e29f7ecfcc59",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "source": [
    "Define `URL`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "93451e07-90ae-454c-8a8b-f0cbeffebd1b",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'https://sls.api.stw-on.de/v1/location/101/menu/2023-12-23/2023-12-29'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "API_URL = \"https://sls.api.stw-on.de/v1/location/101/menu/{}/{}\".format(\n",
    "    today + timedelta(days = 1), today + timedelta(days = 7))\n",
    "API_URL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "a83c65f0-8e9f-4974-a79d-17fc400ad342",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "API_URL = \"https://sls.api.stw-on.de/v1/location/101/menu/{}/{}\".format(\n",
    "    today - timedelta(days = 5), today)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7dd1c1b4-09c7-42dc-b840-f1e343ec4dc6",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "Wake a response and convert to json:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "a00527d4-a92c-436a-9936-287d4db88cbe",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "resp = requests.get(API_URL)\n",
    "resp_json = resp.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba187080-7eaa-46fa-83f5-1ea60e6c675d",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "source": [
    "Extract Meals Data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "0a9f352b-8951-40fc-a477-b2d4384aa4aa",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'student': '2.05', 'employee': '3.75', 'guest': '4.85'}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "meals = resp_json.get(\"meals\")\n",
    "meals[0]['price']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7691122-e11b-414b-be72-7f50d65967c5",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "Now convert the list of meals into a Pandas DataFrame object and store it in `df_meals`. Make sure that the date column has the datetime `data` type and that the prices are in a numeric (`float`) format.\n",
    "\n",
    "Hint: You might want to check the `pandas.json_normalize` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "e086f924-1031-4d0c-8f7c-4f5cfd96ba68",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "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>id</th>\n",
       "      <th>date</th>\n",
       "      <th>name</th>\n",
       "      <th>name_en</th>\n",
       "      <th>time</th>\n",
       "      <th>special_tags</th>\n",
       "      <th>price.student</th>\n",
       "      <th>price.employee</th>\n",
       "      <th>price.guest</th>\n",
       "      <th>nutritional_values._NOTE</th>\n",
       "      <th>...</th>\n",
       "      <th>location.address.zip</th>\n",
       "      <th>location.address.city</th>\n",
       "      <th>location.opening_hours</th>\n",
       "      <th>lane.id</th>\n",
       "      <th>lane.name</th>\n",
       "      <th>lane.name_en</th>\n",
       "      <th>tags.categories</th>\n",
       "      <th>tags.allergens</th>\n",
       "      <th>tags.additives</th>\n",
       "      <th>tags.special</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>50181</td>\n",
       "      <td>2023-12-18</td>\n",
       "      <td>Paprikasuppe</td>\n",
       "      <td>Bell pepper cream soup</td>\n",
       "      <td>noon</td>\n",
       "      <td>[Deprecated. Use tags→special instead.]</td>\n",
       "      <td>2.05</td>\n",
       "      <td>3.75</td>\n",
       "      <td>4.85</td>\n",
       "      <td>WARNING: These fields currently contain incorr...</td>\n",
       "      <td>...</td>\n",
       "      <td>38106</td>\n",
       "      <td>Braunschweig</td>\n",
       "      <td>[{'time': 'noon', 'start_day': 1, 'end_day': 4...</td>\n",
       "      <td>10</td>\n",
       "      <td>Suppe &amp; Co.</td>\n",
       "      <td>Soup &amp; Co.</td>\n",
       "      <td>[{'id': 'VEGA', 'name': 'Vegan', 'name_en': 'v...</td>\n",
       "      <td>[{'id': 'SO', 'name': 'enthält Soja(bohnen)u d...</td>\n",
       "      <td>[{'id': '2', 'name': 'mit Konservierungsstoff'...</td>\n",
       "      <td>[]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>50183</td>\n",
       "      <td>2023-12-18</td>\n",
       "      <td>Bulgurpfanne mit Wildkräutern | Kräuterjoghurt...</td>\n",
       "      <td>Bulgur with wild herbs and courgettes | Herb y...</td>\n",
       "      <td>noon</td>\n",
       "      <td>[Deprecated. Use tags→special instead.]</td>\n",
       "      <td>2.50</td>\n",
       "      <td>5.80</td>\n",
       "      <td>6.90</td>\n",
       "      <td>WARNING: These fields currently contain incorr...</td>\n",
       "      <td>...</td>\n",
       "      <td>38106</td>\n",
       "      <td>Braunschweig</td>\n",
       "      <td>[{'time': 'noon', 'start_day': 1, 'end_day': 4...</td>\n",
       "      <td>20</td>\n",
       "      <td>Classic 1</td>\n",
       "      <td>Classic 1</td>\n",
       "      <td>[{'id': 'VEGT', 'name': 'Vegetarisch', 'name_e...</td>\n",
       "      <td>[{'id': 'ML', 'name': 'enthält Milch u Milcher...</td>\n",
       "      <td>[]</td>\n",
       "      <td>[]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 33 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      id       date                                               name  \\\n",
       "0  50181 2023-12-18                                       Paprikasuppe   \n",
       "1  50183 2023-12-18  Bulgurpfanne mit Wildkräutern | Kräuterjoghurt...   \n",
       "\n",
       "                                             name_en  time  \\\n",
       "0                             Bell pepper cream soup  noon   \n",
       "1  Bulgur with wild herbs and courgettes | Herb y...  noon   \n",
       "\n",
       "                              special_tags  price.student price.employee  \\\n",
       "0  [Deprecated. Use tags→special instead.]           2.05           3.75   \n",
       "1  [Deprecated. Use tags→special instead.]           2.50           5.80   \n",
       "\n",
       "  price.guest                           nutritional_values._NOTE  ...  \\\n",
       "0        4.85  WARNING: These fields currently contain incorr...  ...   \n",
       "1        6.90  WARNING: These fields currently contain incorr...  ...   \n",
       "\n",
       "  location.address.zip location.address.city  \\\n",
       "0                38106          Braunschweig   \n",
       "1                38106          Braunschweig   \n",
       "\n",
       "                              location.opening_hours lane.id    lane.name  \\\n",
       "0  [{'time': 'noon', 'start_day': 1, 'end_day': 4...      10  Suppe & Co.   \n",
       "1  [{'time': 'noon', 'start_day': 1, 'end_day': 4...      20    Classic 1   \n",
       "\n",
       "  lane.name_en                                    tags.categories  \\\n",
       "0   Soup & Co.  [{'id': 'VEGA', 'name': 'Vegan', 'name_en': 'v...   \n",
       "1    Classic 1  [{'id': 'VEGT', 'name': 'Vegetarisch', 'name_e...   \n",
       "\n",
       "                                      tags.allergens  \\\n",
       "0  [{'id': 'SO', 'name': 'enthält Soja(bohnen)u d...   \n",
       "1  [{'id': 'ML', 'name': 'enthält Milch u Milcher...   \n",
       "\n",
       "                                      tags.additives tags.special  \n",
       "0  [{'id': '2', 'name': 'mit Konservierungsstoff'...           []  \n",
       "1                                                 []           []  \n",
       "\n",
       "[2 rows x 33 columns]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_meals = pd.json_normalize(meals)\n",
    "df_meals[\"date\"] = pd.to_datetime(df_meals[\"date\"])\n",
    "df_meals[\"price.student\"] = pd.to_numeric(df_meals[\"price.student\"])\n",
    "df_meals.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19fcfa4a-2dfd-443d-84af-5519d2eb61a1",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "Next, create a simple plot that plots the student prices over time. You can use a scatter plot to do this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "039c0393-c015-4f83-8093-5d3a37d11245",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 18444 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(df_meals[\"date\"], df_meals[\"price.student\"])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01fc72e6-3dc3-4c75-8cd4-1eaa4f3c2dbb",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Task 2\n",
    "\n",
    "Next, make a request to the Mensa API to get all meals in the time frame December 1, 2023 to December 21, 2023.\n",
    "\n",
    "After requesting the data, Analyze it by their mean price Distribution, and a thing you find interesting to analyze. (Be Creative)\n",
    "\n",
    "What can you observe?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "6dcd8328-771a-47a4-accd-d1945118d81e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "40"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "API_URL = \"https://sls.api.stw-on.de/v1/location/101/menu/2023-12-01/2023-12-21\"\n",
    "\n",
    "resp = requests.get(API_URL)\n",
    "data = resp.json()\n",
    "\n",
    "meals=data.get(\"meals\")\n",
    "len(meals)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "3b98f3ae-1f53-4330-a62d-3f4926d74456",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "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>id</th>\n",
       "      <th>date</th>\n",
       "      <th>name</th>\n",
       "      <th>name_en</th>\n",
       "      <th>time</th>\n",
       "      <th>special_tags</th>\n",
       "      <th>price.student</th>\n",
       "      <th>price.employee</th>\n",
       "      <th>price.guest</th>\n",
       "      <th>nutritional_values._NOTE</th>\n",
       "      <th>...</th>\n",
       "      <th>location.name</th>\n",
       "      <th>location.address</th>\n",
       "      <th>location.opening_hours</th>\n",
       "      <th>lane.id</th>\n",
       "      <th>lane.name</th>\n",
       "      <th>lane.name_en</th>\n",
       "      <th>tags.categories</th>\n",
       "      <th>tags.allergens</th>\n",
       "      <th>tags.additives</th>\n",
       "      <th>tags.special</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>50181</td>\n",
       "      <td>2023-12-18</td>\n",
       "      <td>Paprikasuppe</td>\n",
       "      <td>Bell pepper cream soup</td>\n",
       "      <td>noon</td>\n",
       "      <td>[Deprecated. Use tags→special instead.]</td>\n",
       "      <td>2.05</td>\n",
       "      <td>3.75</td>\n",
       "      <td>4.85</td>\n",
       "      <td>WARNING: These fields currently contain incorr...</td>\n",
       "      <td>...</td>\n",
       "      <td>Mensa 1 TU Braunschweig</td>\n",
       "      <td>{'line1': 'Mensa 1 TU Braunschweig', 'line2': ...</td>\n",
       "      <td>[{'time': 'noon', 'start_day': 1, 'end_day': 4...</td>\n",
       "      <td>10</td>\n",
       "      <td>Suppe &amp; Co.</td>\n",
       "      <td>Soup &amp; Co.</td>\n",
       "      <td>[{'id': 'VEGA', 'name': 'Vegan', 'name_en': 'v...</td>\n",
       "      <td>[{'id': 'SO', 'name': 'enthält Soja(bohnen)u d...</td>\n",
       "      <td>[{'id': '2', 'name': 'mit Konservierungsstoff'...</td>\n",
       "      <td>[]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 22 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      id       date          name                 name_en  time  \\\n",
       "0  50181 2023-12-18  Paprikasuppe  Bell pepper cream soup  noon   \n",
       "\n",
       "                              special_tags  price.student  price.employee  \\\n",
       "0  [Deprecated. Use tags→special instead.]           2.05            3.75   \n",
       "\n",
       "   price.guest                           nutritional_values._NOTE  ...  \\\n",
       "0         4.85  WARNING: These fields currently contain incorr...  ...   \n",
       "\n",
       "             location.name                                   location.address  \\\n",
       "0  Mensa 1 TU Braunschweig  {'line1': 'Mensa 1 TU Braunschweig', 'line2': ...   \n",
       "\n",
       "                              location.opening_hours lane.id    lane.name  \\\n",
       "0  [{'time': 'noon', 'start_day': 1, 'end_day': 4...      10  Suppe & Co.   \n",
       "\n",
       "   lane.name_en                                    tags.categories  \\\n",
       "0    Soup & Co.  [{'id': 'VEGA', 'name': 'Vegan', 'name_en': 'v...   \n",
       "\n",
       "                                      tags.allergens  \\\n",
       "0  [{'id': 'SO', 'name': 'enthält Soja(bohnen)u d...   \n",
       "\n",
       "                                      tags.additives tags.special  \n",
       "0  [{'id': '2', 'name': 'mit Konservierungsstoff'...           []  \n",
       "\n",
       "[1 rows x 22 columns]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_meals=pd.json_normalize(meals, max_level=1)\n",
    "df_meals=df_meals.astype({'price.student': 'float',\"price.employee\":\"float\", \"price.guest\":\"float\"})\n",
    "df_meals[\"date\"]=df_meals[\"date\"].astype('datetime64[ns]')\n",
    "df_meals.head(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3995a8a9-6e02-4228-a288-8708a7fedffd",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "Analyzing Price Distribution:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "08bcf84c-32c3-4afe-b333-6863acd31dc6",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "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>date</th>\n",
       "      <th>id</th>\n",
       "      <th>price.student</th>\n",
       "      <th>price.employee</th>\n",
       "      <th>price.guest</th>\n",
       "      <th>location.id</th>\n",
       "      <th>lane.id</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-12-18</th>\n",
       "      <td>2023-12-18</td>\n",
       "      <td>50181</td>\n",
       "      <td>2.020</td>\n",
       "      <td>3.185</td>\n",
       "      <td>3.700</td>\n",
       "      <td>101</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-19</th>\n",
       "      <td>2023-12-19</td>\n",
       "      <td>50189</td>\n",
       "      <td>1.975</td>\n",
       "      <td>3.235</td>\n",
       "      <td>3.715</td>\n",
       "      <td>101</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-20</th>\n",
       "      <td>2023-12-20</td>\n",
       "      <td>50397</td>\n",
       "      <td>2.020</td>\n",
       "      <td>3.245</td>\n",
       "      <td>3.760</td>\n",
       "      <td>101</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-21</th>\n",
       "      <td>2023-12-21</td>\n",
       "      <td>50225</td>\n",
       "      <td>2.135</td>\n",
       "      <td>3.300</td>\n",
       "      <td>3.815</td>\n",
       "      <td>101</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 date     id  price.student  price.employee  price.guest  \\\n",
       "date                                                                       \n",
       "2023-12-18 2023-12-18  50181          2.020           3.185        3.700   \n",
       "2023-12-19 2023-12-19  50189          1.975           3.235        3.715   \n",
       "2023-12-20 2023-12-20  50397          2.020           3.245        3.760   \n",
       "2023-12-21 2023-12-21  50225          2.135           3.300        3.815   \n",
       "\n",
       "            location.id  lane.id  \n",
       "date                              \n",
       "2023-12-18          101       10  \n",
       "2023-12-19          101       10  \n",
       "2023-12-20          101       10  \n",
       "2023-12-21          101       10  "
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "aggregation_functions = {'date': 'first',\n",
    "                         'id': 'first',\n",
    "                         'price.student': 'mean',\n",
    "                         'price.employee':\"mean\",\n",
    "                         \"price.guest\":\"mean\",\n",
    "                         \"location.id\":\"first\",\n",
    "                         \"lane.id\":\"first\"}\n",
    "df_new = df_meals.groupby(df_meals['date']).aggregate(aggregation_functions)\n",
    "df_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "72fdef8c-be30-4a8c-984f-6046a7fe77c4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 29500 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#mean_price.plot\n",
    "plt.plot(df_new[\"date\"], df_new[\"price.student\"], color=\"yellow\",label=\"student\")  \n",
    "plt.plot(df_new[\"date\"], df_new[\"price.employee\"],color= \"red\", label=\"employee\")\n",
    "plt.plot(df_new[\"date\"], df_new[\"price.guest\"], color= \"purple\", label=\"guest\")\n",
    "plt.title('Prices Mensa over time')  \n",
    "plt.xlabel('Date')  \n",
    "plt.ylabel('Price')  \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93879add-8854-4170-8962-13b2fefaa686",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "# Data Analysis"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cde1855d-ddf6-487f-a02f-5e75bafffcc6",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "23efc8a6-5c1c-45c5-90a7-4c8d70f59fc8",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15a624b0-53bf-4bc8-a1a6-4bb499d74af4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "Next read in the dataset `survey.csv`. For testing puposes use the Variable Name `df`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "e1064073-6a61-4d49-9984-3f244462d6c2",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "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>Age</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Scale Python Exp</th>\n",
       "      <th>Course</th>\n",
       "      <th>Has Voice Assistent Contact</th>\n",
       "      <th>Voice Assistent</th>\n",
       "      <th>Scale Study Satisfaction</th>\n",
       "      <th>Uses Smartphone</th>\n",
       "      <th>Which Smartphone</th>\n",
       "      <th>Has Computer</th>\n",
       "      <th>Which OS</th>\n",
       "      <th>Scale Programming Exp</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>22</td>\n",
       "      <td>Männlich</td>\n",
       "      <td>4</td>\n",
       "      <td>Medienwissenschaften</td>\n",
       "      <td>Ja</td>\n",
       "      <td>Apple Siri</td>\n",
       "      <td>4</td>\n",
       "      <td>Ja</td>\n",
       "      <td>Apple</td>\n",
       "      <td>Ja</td>\n",
       "      <td>Mac OS</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>26</td>\n",
       "      <td>Weiblich</td>\n",
       "      <td>3</td>\n",
       "      <td>Medienwissenschaften</td>\n",
       "      <td>Ja</td>\n",
       "      <td>Amazon Alexa</td>\n",
       "      <td>2</td>\n",
       "      <td>Ja</td>\n",
       "      <td>Xiaomi</td>\n",
       "      <td>Ja</td>\n",
       "      <td>Windows 10</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>21</td>\n",
       "      <td>Männlich</td>\n",
       "      <td>3</td>\n",
       "      <td>Medienwissenschaften</td>\n",
       "      <td>Ja</td>\n",
       "      <td>Google Now</td>\n",
       "      <td>4</td>\n",
       "      <td>Ja</td>\n",
       "      <td>Sonstige</td>\n",
       "      <td>Ja</td>\n",
       "      <td>Windows 10</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>26</td>\n",
       "      <td>Weiblich</td>\n",
       "      <td>4</td>\n",
       "      <td>Medienwissenschaften</td>\n",
       "      <td>Ja</td>\n",
       "      <td>Apple Siri</td>\n",
       "      <td>4</td>\n",
       "      <td>Ja</td>\n",
       "      <td>Samsung</td>\n",
       "      <td>Ja</td>\n",
       "      <td>Windows 10</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>24</td>\n",
       "      <td>Weiblich</td>\n",
       "      <td>4</td>\n",
       "      <td>Psychologie</td>\n",
       "      <td>Nein</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4</td>\n",
       "      <td>Ja</td>\n",
       "      <td>Apple</td>\n",
       "      <td>Ja</td>\n",
       "      <td>Windows 11</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Age       Sex  Scale Python Exp                Course  \\\n",
       "0   22  Männlich                 4  Medienwissenschaften   \n",
       "1   26  Weiblich                 3  Medienwissenschaften   \n",
       "2   21  Männlich                 3  Medienwissenschaften   \n",
       "3   26  Weiblich                 4  Medienwissenschaften   \n",
       "4   24  Weiblich                 4           Psychologie   \n",
       "\n",
       "  Has Voice Assistent Contact Voice Assistent  Scale Study Satisfaction  \\\n",
       "0                          Ja      Apple Siri                         4   \n",
       "1                          Ja    Amazon Alexa                         2   \n",
       "2                          Ja      Google Now                         4   \n",
       "3                          Ja      Apple Siri                         4   \n",
       "4                        Nein             NaN                         4   \n",
       "\n",
       "  Uses Smartphone Which Smartphone Has Computer    Which OS  \\\n",
       "0              Ja            Apple           Ja      Mac OS   \n",
       "1              Ja           Xiaomi           Ja  Windows 10   \n",
       "2              Ja         Sonstige           Ja  Windows 10   \n",
       "3              Ja          Samsung           Ja  Windows 10   \n",
       "4              Ja            Apple           Ja  Windows 11   \n",
       "\n",
       "   Scale Programming Exp  \n",
       "0                      2  \n",
       "1                      3  \n",
       "2                      3  \n",
       "3                      2  \n",
       "4                      3  "
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(\"survey.csv\")\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6703c75-1795-4a02-88bd-001961a3e30e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "# Hypothesis 1\n",
    "\n",
    "__The average age in the course is 25.34 years, with a surplus of females.__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "85f41410-fa96-4d27-963a-2eecc1dbb8ed",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mean Age: 22.64\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 14268 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(\"Mean Age:\", df[\"Age\"].mean())\n",
    "c = df[\"Sex\"].value_counts()\n",
    "plt.title(\"Gender Distribution\")\n",
    "plt.bar(c.keys(), c, color=[\"black\", \"white\"])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84ad4ec5-809e-4d50-9ac4-d0e55998b4f2",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "# Hypothesis 2\n",
    "\n",
    "__The most used voice assistant is Alexa.__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "1907f74b-b7bf-48f2-a544-f5acc4dcd2a3",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 17944 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "c = df[\"Voice Assistent\"].value_counts()\n",
    "plt.title(\"Voice Assistent Distribution\")\n",
    "plt.bar(c.keys(), c, color=[\"red\", \"green\", \"blue\"])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "950b588b-6253-428e-a4a3-9efd6f9b9d3f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "# Hypothesis 3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6dbaf7e5-a6a3-4dd6-be4d-cf483f6bd240",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Hypothesis 3.1\n",
    "\n",
    "__The least used smartphone operating system is iOS.__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "68960c84-031a-44de-8904-475a4a2deddf",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "os_dict = {\n",
    "    \"iOS\": 0,\n",
    "    \"Android\": 0\n",
    "}\n",
    "\n",
    "for data in df[\"Which Smartphone\"]:\n",
    "    if data == \"Apple\":\n",
    "        os_dict[\"iOS\"] += 1\n",
    "    else:\n",
    "        os_dict[\"Android\"] += 1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "8cbd40a2-64e8-4d3b-99bb-35fbf2a1c471",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 13628 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.title(\"Mobile OS Distribution\")\n",
    "plt.bar(os_dict.keys(), os_dict.values(), color=[\"silver\", \"green\"])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db9f1a22-9e5f-4efe-a2f4-096c78d4a353",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Hypothesis 3.2\n",
    "\n",
    "__The most used desktop operating system is Windows 10.__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "7ed466a2-ec98-49f3-8b74-3e95f3c0b3fd",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 15688 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "os_dict = df[\"Which OS\"].value_counts()\n",
    "plt.title(\"Desktop OS Distribution\")\n",
    "plt.bar(os_dict.keys(), os_dict, color=[\"grey\", \"silver\", \"cyan\", \"yellow\"])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c98adc74-bfd1-4b3a-9f21-030547052302",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "# Hypothesis 4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07ba83d7-55d9-4c38-a82b-b6b3751e6354",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Hypothesis 4.1 & 4.2\n",
    "\n",
    "__The youngest people use an iPhone.__\n",
    "__Older people use an Android-based smartphone.__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "312838f3-3191-47ad-88d4-cae46482b7ce",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "age_mean = df[\"Age\"].mean()\n",
    "youngest = {\n",
    "    \"ios\": 0,\n",
    "    \"Android\": 0,\n",
    "    \"n\": 0,\n",
    "    \"ratio iOS\": 0,\n",
    "    \"ratio Android\": 0\n",
    "}\n",
    "\n",
    "oldest = {\n",
    "    \"ios\": 0,\n",
    "    \"Android\": 0,\n",
    "    \"n\": 0,\n",
    "    \"ratio iOS\": 0,\n",
    "    \"ratio Android\": 0\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "edb0864e-9890-4c4a-92ca-f275dfbd658b",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "Count appreances:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "fb13bec6-0536-4268-9fe1-a7ccdc177d76",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "for index, row in df[[\"Age\", \"Which Smartphone\"]].iterrows():\n",
    "    if age_mean > row[\"Age\"]:\n",
    "        youngest[\"n\"] += 1\n",
    "        if row[\"Which Smartphone\"] == \"Apple\":\n",
    "            youngest[\"ios\"] += 1\n",
    "        else:\n",
    "            youngest[\"Android\"] += 1\n",
    "\n",
    "    if age_mean < row[\"Age\"]:\n",
    "        oldest[\"n\"] += 1\n",
    "        if row[\"Which Smartphone\"] != \"Apple\":\n",
    "            oldest[\"Android\"] += 1\n",
    "        else:\n",
    "            oldest[\"ios\"] += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d12d9530-4b49-4d9d-ba96-e1e49a2eada2",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "Calc Ratios:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "aef9b0fe-064c-4e09-9013-9ee1e53bf27b",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "youngest[\"ratio iOS\"] = youngest[\"ios\"] / youngest[\"n\"] * 100\n",
    "youngest[\"ratio Android\"] = youngest[\"Android\"] / youngest[\"n\"] * 100\n",
    "\n",
    "oldest[\"ratio iOS\"] = oldest[\"ios\"] / oldest[\"n\"] * 100\n",
    "oldest[\"ratio Android\"] = oldest[\"Android\"] / oldest[\"n\"] * 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "1f051e34-6a14-4e75-8a4c-4d4bc76db3ca",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 20448 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.title(\"iOS Ratio between mean Age\")\n",
    "plt.bar([\"Y: mean iOS\", \"Y: mean Android\", \"O: mean iOS\", \"O: mean Android\"], \n",
    "        [youngest[\"ratio iOS\"], youngest[\"ratio Android\"], oldest[\"ratio iOS\"], oldest[\"ratio Android\"]], color=[\"gold\", \"silver\"])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e537484-c541-416e-8b25-d7b4a75e1e2c",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": []
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
