Skip to content
Snippets Groups Projects
Uebung_5_LSG.ipynb 351 KiB
Newer Older
Daniel Hahn's avatar
Daniel Hahn committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "03c85993",
   "metadata": {},
   "source": [
    "# <font color='blue'>**Übung 5 - Datenanalyse - Pandas**</font>\n",
    "(Diese Übung gehört zur Vorlesungseinheit 6)\n",
    "\n",
    "## <font color='blue'>**Problemstellung: Analyse von PKW-Verbrauchsdaten**</font>\n",
    "### <font color='blue'>**Problembeschreibung**</font>\n",
    "\n",
    "Eine umfangreiche frei verfügbare Datenbank über Verbrauchsdaten von gut 46.000 PKW-Modelle wird von der US-Regierung unter  https://www.fueleconomy.gov/feg/ws/index.shtml zur Verfügung gestellt (Eine bereits etwas aufbereitete Form liegt beweits als \"vehicles.csv\" im Verzeichnis dieser Übung). Diese soll mithilfe des Pakets pandas weiter aufbereitet und untersucht werden. Der Verbrauch ist wie in den USA üblich in **miles per gallon** angegeben. Dies soll zu **l/100km** umgewandelt werden. Außerdem sollen Datenlücken sinnvoll gefüllt werden. Der **Hubraum** (displ) ist bereits in Liter angegeben. Wenn in dieser Übung ohne weitere Angabe von **Verbrauch** gesprochen wird, ist der **kombinierte Verbrauch** (Stadt- und Land) gemeint. **Fueltype** in der bereitgestellten Datenbasis bezieht sich auf den **Primärkraftstoff** (Hybridfahrzeuge erscheinen als Verbenner). Bei Elektrofahrzeugen ist der Verbrauch in **miles per gallon gasoline equivalents** angegeben (Hintergrund für Interessierte https://www.caranddriver.com/research/a31863350/mpge/).\n",
    "\n",
    "Nach einer grundsätzlichen Vertrautmachung mit der Datenbasis sollen folgende **Fragestellungen** beantwortet werden:\n",
    "1) Zusammenhang zwischen Hubraum und Verbrauch, sowie Jahr und Verbrauch bei allen PKW (z.B. Korrelation, Scatter-plot, Liniendiagramm mit Median-Verbrauch über die Zeit)\n",
    "2) Zusammenhang der Verbrauchsdaten vom verwendeten Kraftstoff (Boxplot mit Quartilen)\n",
    "3) Vergleich der Verbrauchsdaten der deutschen Marken Audi, BMW, Mercedes-Benz, Porsche und Volkswagen (Boxplot mit Quartilen)\n",
    "4) Vergleich der Verbrauchsdaten der 5 häufigsten Fahrzeugklassen (Boxplot mit Quartilen, Klassen automatisiert ermitteln)\n",
    "5) Ermitteln der 15 verbrauchsarmsten Fahrzeuge eines gewählten Herstellers (oder Klasse) unter Ausschluss bestimmter Kraftstoffarten (z.B. ausgenommen Elektrofahrzeuge)\n",
    "\n",
    "### <font color='blue'>**Modellbildung und Algorithmierung**</font>\n",
    "\n",
    "Das Paket Pandas übernimmt die Methoden zur Datenspeicherung und Auswertung, die wir zum Beantworten der Fragestellungen verwenden und zum Teil kombinieren müssen. Dies wird im Bereich Umsetzung für jede der Fragen separat erklärt. Dies passt zu der eher interaktiven Anwendung bei der Datenauswertung eines neuen Datensatzes. Hat man regelmäßig Datensätze nach gleichem Format, können die pandas-Methoden natürlich auch in Algorithmen verwendet werden (z.B. monatliche Statistiken über Verkaufszahlen).\n",
    "\n",
    "### <font color='blue'>**Umsetzung**</font>\n",
    "\n",
    "Zunächst importieren wir das Paket pandas und lesen die CSV-Datei in einen Dataframe ein (Da wir später hauptsächlich in einer Kopie arbeiten, nennen wir diesen z.B. `df_orig`). Von diesem lassen wir uns zunächst die ersten Einträge anzeigen, um den Aufbau der Datenbank zu erkennen."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2d37eba9",
   "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>make</th>\n",
       "      <th>model</th>\n",
       "      <th>year</th>\n",
       "      <th>VClass</th>\n",
       "      <th>cylinders</th>\n",
       "      <th>displ</th>\n",
       "      <th>fuelType</th>\n",
       "      <th>city</th>\n",
       "      <th>highway</th>\n",
       "      <th>combined</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></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>0</th>\n",
       "      <td>Alfa Romeo</td>\n",
       "      <td>Spider Veloce 2000</td>\n",
       "      <td>1985</td>\n",
       "      <td>Two Seaters</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>Regular Gasoline</td>\n",
       "      <td>19</td>\n",
       "      <td>25</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Ferrari</td>\n",
       "      <td>Testarossa</td>\n",
       "      <td>1985</td>\n",
       "      <td>Two Seaters</td>\n",
       "      <td>12.0</td>\n",
       "      <td>4.9</td>\n",
       "      <td>Regular Gasoline</td>\n",
       "      <td>9</td>\n",
       "      <td>14</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Dodge</td>\n",
       "      <td>Charger</td>\n",
       "      <td>1985</td>\n",
       "      <td>Subcompact Cars</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.2</td>\n",
       "      <td>Regular Gasoline</td>\n",
       "      <td>23</td>\n",
       "      <td>33</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Dodge</td>\n",
       "      <td>B150/B250 Wagon 2WD</td>\n",
       "      <td>1985</td>\n",
       "      <td>Vans</td>\n",
       "      <td>8.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>Regular Gasoline</td>\n",
       "      <td>10</td>\n",
       "      <td>12</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Subaru</td>\n",
       "      <td>Legacy AWD Turbo</td>\n",
       "      <td>1993</td>\n",
       "      <td>Compact Cars</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.2</td>\n",
       "      <td>Premium Gasoline</td>\n",
       "      <td>17</td>\n",
       "      <td>23</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          make                model  year           VClass  cylinders  displ  \\\n",
       "id                                                                             \n",
       "0   Alfa Romeo   Spider Veloce 2000  1985      Two Seaters        4.0    2.0   \n",
       "1      Ferrari           Testarossa  1985      Two Seaters       12.0    4.9   \n",
       "2        Dodge              Charger  1985  Subcompact Cars        4.0    2.2   \n",
       "3        Dodge  B150/B250 Wagon 2WD  1985             Vans        8.0    5.2   \n",
       "4       Subaru     Legacy AWD Turbo  1993     Compact Cars        4.0    2.2   \n",
       "\n",
       "            fuelType  city  highway  combined  \n",
       "id                                             \n",
       "0   Regular Gasoline    19       25        21  \n",
       "1   Regular Gasoline     9       14        11  \n",
       "2   Regular Gasoline    23       33        27  \n",
       "3   Regular Gasoline    10       12        11  \n",
       "4   Premium Gasoline    17       23        19  "
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df_orig = pd.read_csv(\"vehicles.csv\", index_col=0)\n",
    "df_orig.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eda1a1b9",
   "metadata": {},
   "source": [
    "Für jedes Fahrzeugmodell stehen die Einträge **id** (fortlaufende Nummer), **make** (Hersteller), **model** (Modell), **year** (Jahr), **VClass** (Fahrzeugtyp/Fahrzeugklasse), **cylinders** (Zylinderanzahl), **displ** (Hubraum in l), **fuelType** (primärer Kraftstoff), **city**, **highway** und **combined** (Verbrauch in mpg für Stadt, Land und kombiniert) zur Verfügung.\n",
    "\n",
    "#### <font color='blue'>**Aufbereitung**</font>\n",
    "\n",
    "Als erstes sollen die Daten aufbereitet werden. Wir definieren dazu eine Funktion, die den Verbrauch in mpg zu l/100km, gerundet auf eine Nachkommastelle, umrechnet. Dazu benötigen wir die Umrechnungsfaktoren:\n",
    "\n",
    "| Imperial | Metrisch |\n",
    "| :--- | :--- |\n",
    "| 1 mile | 1.61 km |\n",
    "| 1 gallon | 3.79 l | "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "801061e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def mpg_to_lp100km(mpg):\n",
    "    return round(379./(mpg*1.61), 1) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf5e39b2",
   "metadata": {},
   "source": [
    "Diese Funktion können wir nun mit `apply`auf die Einträge der Verbauchsspalten anwenden. Dazu kopieren wir zunächst den eingelesenen Dataframe, um die Originaldaten nicht zu beeinflussen. Die ermittelten Spalten werden in der Kopie gespeichert und ersetzen den ursprünglichen Wert. Wir prüfen den Datensatz, um zu erkennen, ob es funktioniert hat."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b905c9a2",
   "metadata": {
    "scrolled": true
   },
   "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>make</th>\n",
       "      <th>model</th>\n",
       "      <th>year</th>\n",
       "      <th>VClass</th>\n",
       "      <th>cylinders</th>\n",
       "      <th>displ</th>\n",
       "      <th>fuelType</th>\n",
       "      <th>city</th>\n",
       "      <th>highway</th>\n",
       "      <th>combined</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></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>0</th>\n",
       "      <td>Alfa Romeo</td>\n",
       "      <td>Spider Veloce 2000</td>\n",
       "      <td>1985</td>\n",
       "      <td>Two Seaters</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>Regular Gasoline</td>\n",
       "      <td>12.4</td>\n",
       "      <td>9.4</td>\n",
       "      <td>11.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Ferrari</td>\n",
       "      <td>Testarossa</td>\n",
       "      <td>1985</td>\n",
       "      <td>Two Seaters</td>\n",
       "      <td>12.0</td>\n",
       "      <td>4.9</td>\n",
       "      <td>Regular Gasoline</td>\n",
       "      <td>26.2</td>\n",
       "      <td>16.8</td>\n",
       "      <td>21.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Dodge</td>\n",
       "      <td>Charger</td>\n",
       "      <td>1985</td>\n",
       "      <td>Subcompact Cars</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.2</td>\n",
       "      <td>Regular Gasoline</td>\n",
       "      <td>10.2</td>\n",
       "      <td>7.1</td>\n",
       "      <td>8.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Dodge</td>\n",
       "      <td>B150/B250 Wagon 2WD</td>\n",
       "      <td>1985</td>\n",
       "      <td>Vans</td>\n",
       "      <td>8.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>Regular Gasoline</td>\n",
       "      <td>23.5</td>\n",
       "      <td>19.6</td>\n",
       "      <td>21.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Subaru</td>\n",
       "      <td>Legacy AWD Turbo</td>\n",
       "      <td>1993</td>\n",
       "      <td>Compact Cars</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.2</td>\n",
       "      <td>Premium Gasoline</td>\n",
       "      <td>13.8</td>\n",
       "      <td>10.2</td>\n",
       "      <td>12.4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          make                model  year           VClass  cylinders  displ  \\\n",
       "id                                                                             \n",
       "0   Alfa Romeo   Spider Veloce 2000  1985      Two Seaters        4.0    2.0   \n",
       "1      Ferrari           Testarossa  1985      Two Seaters       12.0    4.9   \n",
       "2        Dodge              Charger  1985  Subcompact Cars        4.0    2.2   \n",
       "3        Dodge  B150/B250 Wagon 2WD  1985             Vans        8.0    5.2   \n",
       "4       Subaru     Legacy AWD Turbo  1993     Compact Cars        4.0    2.2   \n",
       "\n",
       "            fuelType  city  highway  combined  \n",
       "id                                             \n",
       "0   Regular Gasoline  12.4      9.4      11.2  \n",
       "1   Regular Gasoline  26.2     16.8      21.4  \n",
       "2   Regular Gasoline  10.2      7.1       8.7  \n",
       "3   Regular Gasoline  23.5     19.6      21.4  \n",
       "4   Premium Gasoline  13.8     10.2      12.4  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df_orig.copy()\n",
    "df['city'] = df_orig['city'].apply(mpg_to_lp100km)\n",
    "df['highway'] = df_orig['highway'].apply(mpg_to_lp100km)\n",
    "df['combined'] = df_orig['combined'].apply(mpg_to_lp100km)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e87738ba",
   "metadata": {},
   "source": [
    "Als nächstes sollen Datenlücken behandelt werden. Zum Finden der Lücken hilft die `info`-Funktion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "90cc4bae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 46186 entries, 0 to 46185\n",
      "Data columns (total 10 columns):\n",
      " #   Column     Non-Null Count  Dtype  \n",
      "---  ------     --------------  -----  \n",
      " 0   make       46186 non-null  object \n",
      " 1   model      46186 non-null  object \n",
      " 2   year       46186 non-null  int64  \n",
      " 3   VClass     46186 non-null  object \n",
      " 4   cylinders  45680 non-null  float64\n",
      " 5   displ      45682 non-null  float64\n",
      " 6   fuelType   46186 non-null  object \n",
      " 7   city       46186 non-null  float64\n",
      " 8   highway    46186 non-null  float64\n",
      " 9   combined   46186 non-null  float64\n",
      "dtypes: float64(5), int64(1), object(4)\n",
      "memory usage: 3.9+ MB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8413d12",
   "metadata": {},
   "source": [
    "Man sieht, dass es **46186 Einträge** gibt, für **Zylinderanzahl** und **Hubraum** aber nur **45680** bzw. **45682**. Auch wenn bereits die Vermutung naheliegt, dass diese Einträge zu Elektrofahrzeugen gehören, auf die diese Motordaten nicht zutreffen, untersuchen wir dies, indem wir uns die betreffenden Einträge anzeigen lassen. Dazu nutzen wir die selektive Auswahl und wählen mithilfe von `isnull()` nur die Einträge aus dem Dataframe aus, bei denen der Wert für die Zylinder nicht vorhanden ist. Da wir diesen noch weiter verwenden, speichern wir ihn ab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3b66e4aa",
   "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>make</th>\n",
       "      <th>model</th>\n",
       "      <th>year</th>\n",
       "      <th>VClass</th>\n",
       "      <th>cylinders</th>\n",
       "      <th>displ</th>\n",
       "      <th>fuelType</th>\n",
       "      <th>city</th>\n",
       "      <th>highway</th>\n",
       "      <th>combined</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></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>7138</th>\n",
       "      <td>Nissan</td>\n",
       "      <td>Altra EV</td>\n",
       "      <td>2000</td>\n",
       "      <td>Midsize Station Wagons</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Electricity</td>\n",
       "      <td>2.9</td>\n",
       "      <td>2.6</td>\n",
       "      <td>2.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7139</th>\n",
       "      <td>Toyota</td>\n",
       "      <td>RAV4 EV</td>\n",
       "      <td>2000</td>\n",
       "      <td>Sport Utility Vehicle - 2WD</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Electricity</td>\n",
       "      <td>2.9</td>\n",
       "      <td>3.7</td>\n",
       "      <td>3.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8143</th>\n",
       "      <td>Toyota</td>\n",
       "      <td>RAV4 EV</td>\n",
       "      <td>2001</td>\n",
       "      <td>Sport Utility Vehicle - 2WD</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Electricity</td>\n",
       "      <td>2.9</td>\n",
       "      <td>3.7</td>\n",
       "      <td>3.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8144</th>\n",
       "      <td>Ford</td>\n",
       "      <td>Th!nk</td>\n",
       "      <td>2001</td>\n",
       "      <td>Two Seaters</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Electricity</td>\n",
       "      <td>3.2</td>\n",
       "      <td>4.1</td>\n",
       "      <td>3.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8146</th>\n",
       "      <td>Ford</td>\n",
       "      <td>Explorer USPS Electric</td>\n",
       "      <td>2001</td>\n",
       "      <td>Sport Utility Vehicle - 2WD</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Electricity</td>\n",
       "      <td>5.2</td>\n",
       "      <td>7.1</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40253</th>\n",
       "      <td>Hyundai</td>\n",
       "      <td>Ioniq 6 Long range AWD (18 inch Wheels)</td>\n",
       "      <td>2023</td>\n",
       "      <td>Midsize Cars</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Electricity</td>\n",
       "      <td>1.8</td>\n",
       "      <td>2.1</td>\n",
       "      <td>1.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40254</th>\n",
       "      <td>Hyundai</td>\n",
       "      <td>Ioniq 6 Long range AWD (20 inch Wheels)</td>\n",
       "      <td>2023</td>\n",
       "      <td>Midsize Cars</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Electricity</td>\n",
       "      <td>2.1</td>\n",
       "      <td>2.5</td>\n",
       "      <td>2.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40255</th>\n",
       "      <td>Hyundai</td>\n",
       "      <td>Ioniq 6 Long range RWD (18 inch Wheels)</td>\n",
       "      <td>2023</td>\n",
       "      <td>Midsize Cars</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Electricity</td>\n",
       "      <td>1.5</td>\n",
       "      <td>1.9</td>\n",
       "      <td>1.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40256</th>\n",
       "      <td>Hyundai</td>\n",
       "      <td>Ioniq 6 Long range RWD (20 inch Wheels)</td>\n",
       "      <td>2023</td>\n",
       "      <td>Midsize Cars</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Electricity</td>\n",
       "      <td>1.8</td>\n",
       "      <td>2.2</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40257</th>\n",
       "      <td>Hyundai</td>\n",
       "      <td>Ioniq 6 Standard Range RWD</td>\n",
       "      <td>2023</td>\n",
       "      <td>Midsize Cars</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Electricity</td>\n",
       "      <td>1.6</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>506 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          make                                    model  year  \\\n",
       "id                                                              \n",
       "7138    Nissan                                 Altra EV  2000   \n",
       "7139    Toyota                                  RAV4 EV  2000   \n",
       "8143    Toyota                                  RAV4 EV  2001   \n",
       "8144      Ford                                    Th!nk  2001   \n",
       "8146      Ford                   Explorer USPS Electric  2001   \n",
       "...        ...                                      ...   ...   \n",
       "40253  Hyundai  Ioniq 6 Long range AWD (18 inch Wheels)  2023   \n",
       "40254  Hyundai  Ioniq 6 Long range AWD (20 inch Wheels)  2023   \n",
       "40255  Hyundai  Ioniq 6 Long range RWD (18 inch Wheels)  2023   \n",
       "40256  Hyundai  Ioniq 6 Long range RWD (20 inch Wheels)  2023   \n",
       "40257  Hyundai               Ioniq 6 Standard Range RWD  2023   \n",
       "\n",
       "                            VClass  cylinders  displ     fuelType  city  \\\n",
       "id                                                                        \n",
       "7138        Midsize Station Wagons        NaN    NaN  Electricity   2.9   \n",
       "7139   Sport Utility Vehicle - 2WD        NaN    NaN  Electricity   2.9   \n",
       "8143   Sport Utility Vehicle - 2WD        NaN    NaN  Electricity   2.9   \n",
       "8144                   Two Seaters        NaN    NaN  Electricity   3.2   \n",
       "8146   Sport Utility Vehicle - 2WD        NaN    NaN  Electricity   5.2   \n",
       "...                            ...        ...    ...          ...   ...   \n",
       "40253                 Midsize Cars        NaN    NaN  Electricity   1.8   \n",
       "40254                 Midsize Cars        NaN    NaN  Electricity   2.1   \n",
       "40255                 Midsize Cars        NaN    NaN  Electricity   1.5   \n",
       "40256                 Midsize Cars        NaN    NaN  Electricity   1.8   \n",
       "40257                 Midsize Cars        NaN    NaN  Electricity   1.6   \n",
       "\n",
       "       highway  combined  \n",
       "id                        \n",
       "7138       2.6       2.8  \n",
       "7139       3.7       3.3  \n",
       "8143       3.7       3.3  \n",
       "8144       4.1       3.6  \n",
       "8146       7.1       6.0  \n",
       "...        ...       ...  \n",
       "40253      2.1       1.9  \n",
       "40254      2.5       2.3  \n",
       "40255      1.9       1.7  \n",
       "40256      2.2       2.0  \n",
       "40257      2.0       1.7  \n",
       "\n",
       "[506 rows x 10 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_nulls = df[ df[\"cylinders\"].isnull() ]\n",
    "df_nulls"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d830244",
   "metadata": {},
   "source": [
    "Es sieht so aus, als wären dies alles Elektrofahrzeuge. Um sicher zu sein, lassen wir mithilfe von `describe()` Informationen über die vorhandenen fuelTypes anzeigen."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "474ac122",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count             506\n",
       "unique              2\n",
       "top       Electricity\n",
       "freq              503\n",
       "Name: fuelType, dtype: object"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_nulls[\"fuelType\"].describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b33853f1",
   "metadata": {},
   "source": [
    "Nur 503 der 506 Einträge haben den Fueltype \"Electricity\". Um herauszufinden, was es mit diesen Daten auf sich hat, verwenden wir `unique()` um alle darin vorkommenden Werte zu erhalten."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6c2684f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['Electricity', 'Regular Gasoline'], dtype=object)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_nulls[\"fuelType\"].unique()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8321c2fa",
   "metadata": {},
   "source": [
    "Um die 3 Benzin-Fahrzeuge mit fehlenden Informationen zum Motor anzuzeigen, filtern wir den Dataframe nach dem Fueltype."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fc1c3f5f",
   "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>make</th>\n",
       "      <th>model</th>\n",
       "      <th>year</th>\n",
       "      <th>VClass</th>\n",
       "      <th>cylinders</th>\n",
       "      <th>displ</th>\n",
       "      <th>fuelType</th>\n",
       "      <th>city</th>\n",
       "      <th>highway</th>\n",
       "      <th>combined</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></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>21410</th>\n",
       "      <td>Subaru</td>\n",
       "      <td>RX Turbo</td>\n",
       "      <td>1985</td>\n",
       "      <td>Subcompact Cars</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Regular Gasoline</td>\n",
       "      <td>10.7</td>\n",
       "      <td>8.4</td>\n",
       "      <td>9.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21411</th>\n",
       "      <td>Subaru</td>\n",
       "      <td>RX Turbo</td>\n",
       "      <td>1985</td>\n",
       "      <td>Subcompact Cars</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Regular Gasoline</td>\n",
       "      <td>11.2</td>\n",
       "      <td>8.7</td>\n",
       "      <td>10.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21500</th>\n",
       "      <td>Mazda</td>\n",
       "      <td>RX-7</td>\n",
       "      <td>1986</td>\n",
       "      <td>Two Seaters</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.3</td>\n",
       "      <td>Regular Gasoline</td>\n",
       "      <td>15.7</td>\n",
       "      <td>10.7</td>\n",
       "      <td>13.1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         make     model  year           VClass  cylinders  displ  \\\n",
       "id                                                                 \n",
       "21410  Subaru  RX Turbo  1985  Subcompact Cars        NaN    NaN   \n",
       "21411  Subaru  RX Turbo  1985  Subcompact Cars        NaN    NaN   \n",
       "21500   Mazda      RX-7  1986      Two Seaters        NaN    1.3   \n",
       "\n",
       "               fuelType  city  highway  combined  \n",
       "id                                                \n",
       "21410  Regular Gasoline  10.7      8.4       9.8  \n",
       "21411  Regular Gasoline  11.2      8.7      10.2  \n",
       "21500  Regular Gasoline  15.7     10.7      13.1  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_nulls[ df_nulls[\"fuelType\"] == \"Regular Gasoline\" ]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "300b0882",
   "metadata": {},
   "source": [
    "Hier verzichten wir zunächst darauf, den fehlenden Daten über den Hubraum nachzugehen.\n",
    "\n",
    "Es muss nun entschieden werden, wie wir mit den fehlenden Daten umgehen wollen, um möglichst aussagekräftige Daten zu behalten. \n",
    "\n",
    "Eine Möglichkeit (die wir in dieser Übung verwenden) ist, allen Elektrofahrzeugen bei Zylinderanzahl und Hubraum den Wert `0.0` einzutragen und die wenigen übrigen Datensätze, in denen diese Informationen fehlen, zu entfernen. \n",
    "\n",
    "Wir beginnen mit dem Entfernen aller Einträge, bei denen Zylinder keinen Wert hat, obwohl es keine Elektrofahrzeuge sind. Einträge entfernen können wir mit mit der Dataframe-Methode `drop([liste von indizes], inplace = True)`. Nun benötigen wir eine Liste von Indizes. Wir sehen diese zwar in der Tabelle vom Schritt zuvor, können diese aber auch automatisch ermitteln. Ein Dataframe hat das Attribut `index`, in dem eine Liste aller vorhandenen Indizes gespeichert ist. Wir können also dieses Attribut des im Schritt zuvor gefilterten dataframe verwenden, um der Drop-Methode die Indizes zu übergeben. Wir überprüfen das Ergebnis mit `info()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7f4c738f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 46183 entries, 0 to 46185\n",
      "Data columns (total 10 columns):\n",
      " #   Column     Non-Null Count  Dtype  \n",
      "---  ------     --------------  -----  \n",
      " 0   make       46183 non-null  object \n",
      " 1   model      46183 non-null  object \n",
      " 2   year       46183 non-null  int64  \n",
      " 3   VClass     46183 non-null  object \n",
      " 4   cylinders  45680 non-null  float64\n",
      " 5   displ      45681 non-null  float64\n",
      " 6   fuelType   46183 non-null  object \n",
      " 7   city       46183 non-null  float64\n",
      " 8   highway    46183 non-null  float64\n",
      " 9   combined   46183 non-null  float64\n",
      "dtypes: float64(5), int64(1), object(4)\n",
      "memory usage: 3.9+ MB\n"
     ]
    }
   ],
   "source": [
    "delete_df = df_nulls[ df_nulls[\"fuelType\"] == \"Regular Gasoline\" ]\n",
    "df.drop( delete_df.index, inplace = True )\n",
    "df.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3d9faef",
   "metadata": {},
   "source": [
    "Nun sind diese 3 Fahrzeuge aus dem Dataframe entfernt. Wir haben bisher nicht die fehlenden Hubraum-Daten untersucht. Da wir bereits wissen, dass wir bei den Elektrofahrzeugen die Werte auf `0.0` setzen werden, müssen wir lediglich überprüfen, ob es noch Einträge ohne angegebenen Hubraum gibt, die keine Elektrofahrzeuge sind. Dazu filtern wir den Dataframe mit einer kombinierten Bedingung. (Achtung, im Zusammenhang mit dem Filtern von Dataframes werden die Operatoren `&` für `and` und `|` für `or` benutzt, und jede Bedingung muss in Klammern gesetzt werden). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c8521251",
   "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>make</th>\n",
       "      <th>model</th>\n",
       "      <th>year</th>\n",
       "      <th>VClass</th>\n",
       "      <th>cylinders</th>\n",
       "      <th>displ</th>\n",
       "      <th>fuelType</th>\n",
       "      <th>city</th>\n",
       "      <th>highway</th>\n",
       "      <th>combined</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></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",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [make, model, year, VClass, cylinders, displ, fuelType, city, highway, combined]\n",
       "Index: []"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[ ( df[\"displ\"].isnull() ) & ( df[\"fuelType\"] != \"Electricity\" ) ]"
   ]
  },
  {