{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "19f8cec2-622f-43cd-95e1-baa89283fb88",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "# Lösungen Pandas & Seaborn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "82d381b3-a37f-4739-9760-7902e155fb37",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13695d29-0bcd-4902-af52-df3b0f353d33",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "## Aufgabe - Erstellen eines Dataframes\n",
    "\n",
    "*5 Punkte*\n",
    "\n",
    "Erstellen Sie einen Pandas Data Frame mit dem namen `uni_addr`, nachdem Schema folgender Tabelle:\n",
    "\n",
    "| Address                  | plz                        |\n",
    "|--------------------------|----------------------------|\n",
    "| Johannes-Selenka-Platz 1 | 38118 Braunschweig         |\n",
    "| Universitätspl. 2        | 38106 Braunschweig         |\n",
    "| Harburger Str. 6         | 21614 Buxtehude            |\n",
    "| Adolph-Roemer-Straße 2A  | 38678 Clausthal-Zellerfeld |\n",
    "| Constantiapl. 4          | 26723 Emden                |\n",
    "| Weender Landstraße 3-7   | 37073 Göttingen            |\n",
    "| Wilhelmsplatz 1          | 37073 Göttingen            |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7a142742-a3fa-4719-82f0-b56e4082ced7",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "uni_addr = pd.DataFrame({\n",
    "    \"Address\": [\n",
    "        \"Johannes-Selenka-Platz 1\", \"Universitätspl. 2\",\n",
    "        \"Harburger Str. 6\", \"Adolph-Roemer-Straße 2A\",\n",
    "        \"Constantiapl. 4\", \"Weender Landstraße 3-7\",\n",
    "        \"Wilhelmsplatz 1\"\n",
    "    ],\n",
    "    \"plz\": [\n",
    "        \"38118 Braunschweig\", \"38106 Braunschweig\",\n",
    "        \"21614 Buxtehude\", \"38678 Clausthal-Zellerfeld\",\n",
    "        \"26723 Emden\", \"37073 Göttingen\", \"37073 Göttingen\",\n",
    "    ]\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b14422b3-7306-4158-b335-3531f5c56f62",
   "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>Address</th>\n",
       "      <th>plz</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Johannes-Selenka-Platz 1</td>\n",
       "      <td>38118 Braunschweig</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Universitätspl. 2</td>\n",
       "      <td>38106 Braunschweig</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Harburger Str. 6</td>\n",
       "      <td>21614 Buxtehude</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Adolph-Roemer-Straße 2A</td>\n",
       "      <td>38678 Clausthal-Zellerfeld</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Constantiapl. 4</td>\n",
       "      <td>26723 Emden</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Weender Landstraße 3-7</td>\n",
       "      <td>37073 Göttingen</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Wilhelmsplatz 1</td>\n",
       "      <td>37073 Göttingen</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    Address                         plz\n",
       "0  Johannes-Selenka-Platz 1          38118 Braunschweig\n",
       "1         Universitätspl. 2          38106 Braunschweig\n",
       "2          Harburger Str. 6             21614 Buxtehude\n",
       "3   Adolph-Roemer-Straße 2A  38678 Clausthal-Zellerfeld\n",
       "4           Constantiapl. 4                 26723 Emden\n",
       "5    Weender Landstraße 3-7             37073 Göttingen\n",
       "6           Wilhelmsplatz 1             37073 Göttingen"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "uni_addr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c861e0c-ddae-4249-94d6-903e605a2b82",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "## Aufgabe - Extrahieren einer Series\n",
    "\n",
    "*1 Punkte*\n",
    "\n",
    "Exthahieren Sie die Series `plz` aus dem zuvor erstelltem Data Frame `uni_addr` und speichern Sie ihr Ergebnis in `uni_plz`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1e197cdc-e6d4-4eef-a384-c026b84ec115",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0            38118 Braunschweig\n",
       "1            38106 Braunschweig\n",
       "2               21614 Buxtehude\n",
       "3    38678 Clausthal-Zellerfeld\n",
       "4                   26723 Emden\n",
       "5               37073 Göttingen\n",
       "6               37073 Göttingen\n",
       "Name: plz, dtype: object"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "uni_plz = uni_addr[\"plz\"]\n",
    "uni_plz"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b28feb13-64d9-44f3-bff3-2295dc98ea7f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "## Aufgabe - Read CSV\n",
    "\n",
    "*1 Punkt*\n",
    "\n",
    "Nutzen sie die Funktion `pd.read_csv` um das Datenset `unis_nd.csv` in die Variable `unis_nd` einzulesen.\n",
    "\n",
    "Falls Sie hilfe benötigen lesen Sie gerne die Dokumentation im [Getting Started](https://pandas.pydata.org/docs/getting_started/intro_tutorials/02_read_write.html) Guide.\n",
    "\n",
    "_Hinweis: Die Datei liegt in keinem Ordner, sondern im selben wie dieses Notebook!_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6c613dad-1541-4be4-8661-b515ac4eb357",
   "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>University name</th>\n",
       "      <th>Type of university</th>\n",
       "      <th>Sponsorship</th>\n",
       "      <th>Right of promotion</th>\n",
       "      <th>Founding year</th>\n",
       "      <th>Number of students</th>\n",
       "      <th>Address</th>\n",
       "      <th>lat</th>\n",
       "      <th>lon</th>\n",
       "      <th>plz</th>\n",
       "      <th>pic</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Hochschule für Bildende Künste Braunschweig</td>\n",
       "      <td>Artistic university</td>\n",
       "      <td>public</td>\n",
       "      <td>yes</td>\n",
       "      <td>1963</td>\n",
       "      <td>976.0</td>\n",
       "      <td>Johannes-Selenka-Platz 1</td>\n",
       "      <td>52.257738</td>\n",
       "      <td>10.502315</td>\n",
       "      <td>38118 Braunschweig</td>\n",
       "      <td>https://www.hbk-bs.de/fileadmin/_processed_/5/...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                               University name   Type of university  \\\n",
       "0  Hochschule für Bildende Künste Braunschweig  Artistic university   \n",
       "\n",
       "  Sponsorship Right of promotion  Founding year  Number of students  \\\n",
       "0      public                yes           1963               976.0   \n",
       "\n",
       "                    Address        lat        lon                 plz  \\\n",
       "0  Johannes-Selenka-Platz 1  52.257738  10.502315  38118 Braunschweig   \n",
       "\n",
       "                                                 pic  \n",
       "0  https://www.hbk-bs.de/fileadmin/_processed_/5/...  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "unis_nd = pd.read_csv(\"unis_nd.csv\")\n",
    "unis_nd.head(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f6fee4f-eb87-4233-a706-41a8cc6ce96c",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "## Aufgabe\n",
    "\n",
    "*2 Punkte*\n",
    "\n",
    "Selektieren Sie die Spalten _University name_, _Founding year_ & _Number of students_, speichern sie ihr Ergebnis in der Variablen `select`.\n",
    "\n",
    "Geben Sie danach die ersten 5 Werte von Oben der Selektion aus."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "07d925f9-2b82-4e3c-82bc-043db2853df8",
   "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>University name</th>\n",
       "      <th>Founding year</th>\n",
       "      <th>Number of students</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Hochschule für Bildende Künste Braunschweig</td>\n",
       "      <td>1963</td>\n",
       "      <td>976.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Technische Universität Carolo-Wilhelmina zu Br...</td>\n",
       "      <td>1745</td>\n",
       "      <td>17709.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Hochschule 21</td>\n",
       "      <td>2005</td>\n",
       "      <td>1084.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Technische Universität Clausthal</td>\n",
       "      <td>1775</td>\n",
       "      <td>3446.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Hochschule Emden/Leer</td>\n",
       "      <td>2009</td>\n",
       "      <td>4481.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     University name  Founding year  \\\n",
       "0        Hochschule für Bildende Künste Braunschweig           1963   \n",
       "1  Technische Universität Carolo-Wilhelmina zu Br...           1745   \n",
       "2                                      Hochschule 21           2005   \n",
       "3                   Technische Universität Clausthal           1775   \n",
       "4                              Hochschule Emden/Leer           2009   \n",
       "\n",
       "   Number of students  \n",
       "0               976.0  \n",
       "1             17709.0  \n",
       "2              1084.0  \n",
       "3              3446.0  \n",
       "4              4481.0  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "select = unis_nd[\n",
    "    [\"University name\", \"Founding year\", \"Number of students\"]\n",
    "]\n",
    "select.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d74bab02-9618-45a4-b6a9-db529c1de9a2",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "## Aufgabe\n",
    "\n",
    "*2 Punkte*\n",
    "\n",
    "Nutzen Sie die Funktion `value_counts` und erstellen Sie ein Balkendiagramm, mittels matplotlib, über die Anzahl an Staatlichen und Privaten Hochschulen Niedersachsen.\n",
    "\n",
    "Die dazugehörige Spalte im Datenset ist _Sponsorship_. Finden Sie eine geeignete Darstellung."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8061a97e-a26f-49ef-924e-12894926732e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 17344 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "c = unis_nd[\"Sponsorship\"].value_counts()\n",
    "plt.style.use('https://github.com/dhaitz/matplotlib-stylesheets/raw/master/pitayasmoothie-dark.mplstyle')\n",
    "plt.bar(c.keys(), c, color=[\"black\", \"white\"])\n",
    "plt.title(\"Universitäten nach Finanzierungstatus\")\n",
    "plt.ylabel(\"Anzahl\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c995d6b7-5136-4fbf-b12a-80b9121bad48",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "## Aufgabe\n",
    "\n",
    "*8 Punkte*\n",
    "\n",
    "Erstelle das Dataset `people_in_germany`.\n",
    "\n",
    "Folgendes Szenario:\n",
    "\n",
    "Du bist Part einer Massenhaft angelegten Studie um die bereits bekannten Zahlen des Statistischen Bundesamtes zu überprüfen.\n",
    "\n",
    "Dazu sind 4 Größen bekannt aus den Angaben des Statistischen Bundesamt für die [männliche Population](https://www.destatis.de/DE/Themen/Gesellschaft-Umwelt/Gesundheit/Gesundheitszustand-Relevantes-Verhalten/Tabellen/koerpermasse-maenner.html) & die [weibliche Population](https://www.destatis.de/DE/Themen/Gesellschaft-Umwelt/Gesundheit/Gesundheitszustand-Relevantes-Verhalten/Tabellen/koerpermasse-frauen.html):\n",
    "\n",
    "||Körpergröße (in cm)|Gewicht (in Kg)|\n",
    "|-|-|-|\n",
    "|Männlich|178.9|85.8|\n",
    "|Weiblich|165.8|69.2|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "795b3a33-af97-4671-a131-58a9eb8bcbfb",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "Gehe dabei wie folgt vor:\n",
    "- Treffe annahmen über die Verteilung und finde geeignete Werte für $\\mu$ & $\\sigma$. **Erkläre** deine Annahmen mit einem kurzen Text.\n",
    "- Die Samplegröße beträgt 1000. Sample dementsprechend aus den gegebenen Werten.\n",
    "- Speichere die gesampleten Personen nach dem Schema: |gender|height|weight| in der File `people_in_germany.csv` als csv.\n",
    "  -> Nutze für das `gender` Attribut den Datentyp `bool` mit der Kodierung: `True = female` & `False = male`.\n",
    "- Stelle dein Ergebnis angemessen dar. Das theme `darkgrid` darf nicht verwendet werden.\n",
    "- **Beschreibe** & **Interpretiere** den Plot. Gehe dabei von der Hypothese aus das es **keine** Unterschiede zwischen den Geschlechtern beim samplen geben sollte.\n",
    "\n",
    "*Tipp: Dataclasses erleichtern die Aufgabe ungemein!*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "fd039a25-70cf-4245-a2e0-db38bbaf5d0a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# define dataclass\n",
    "import numpy as np\n",
    "from dataclasses import dataclass\n",
    "@dataclass\n",
    "class Individual:\n",
    "    gender: bool\n",
    "    height: np.float64\n",
    "    weight: np.float64"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "eb36da58-8c37-4bee-82ae-eac31f1ebacc",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# fixed values\n",
    "samples = 1000\n",
    "rng = np.random.default_rng()\n",
    "individuals = list()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d2f1e60a-e670-43b0-91e1-9fae768da4cd",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Simulation part\n",
    "for i in range(samples):\n",
    "    if i % 2 == 0:\n",
    "        gender = False\n",
    "        height = rng.normal(178.9, 10)\n",
    "        weight = rng.normal(85.8, 26)\n",
    "                            \n",
    "    else:\n",
    "        gender = True\n",
    "        height = rng.normal(165.8, 11)\n",
    "        weight = rng.normal(69.2, 19)\n",
    "\n",
    "    height = np.round(height, decimals=1)\n",
    "    weight = np.round(weight, decimals=1)\n",
    "    \n",
    "    individual = Individual(gender=gender,height=height,weight=weight)\n",
    "    individuals.append(individual)\n",
    "\n",
    "individuals = pd.DataFrame(individuals)\n",
    "individuals.to_csv('people_in_germany.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "6bde202f-3900-46d7-a6df-fdedd14cd367",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 149028 characters]",
      "text/plain": [
       "<Figure size 600x600 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.set_style('white')\n",
    "sns.jointplot(data=individuals, x=\"height\", y=\"weight\", hue=\"gender\")\n",
    "plt.show()"
   ]
  }
 ],
 "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.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
