{ "cells": [ { "cell_type": "markdown", "id": "4231b346", "metadata": {}, "source": [ "# Arithmetik\n", "\n", "Eine wichtige Funktion von pandas ist das arithmetische Verhalten bei Objekten mit unterschiedlichen Indizes. Wenn beim Addieren von Objekten die Indexpaare nicht gleich sind, wird der entsprechende Index im Ergebnis die Vereinigung der Indexpaare sein. Für Benutzer mit Datenbankerfahrung ist dies vergleichbar mit einem automatischen [OUTER JOIN](https://de.wikipedia.org/wiki/Join_(SQL)#%C3%84u%C3%9Ferer_Verbund_(OUTER_JOIN)) auf den Indexbezeichnungen. Schauen wir uns ein Beispiel an:" ] }, { "cell_type": "code", "execution_count": 1, "id": "5b29913f", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:19:50.539387Z", "iopub.status.busy": "2026-05-21T16:19:50.539238Z", "iopub.status.idle": "2026-05-21T16:19:50.774627Z", "shell.execute_reply": "2026-05-21T16:19:50.774326Z", "shell.execute_reply.started": "2026-05-21T16:19:50.539369Z" } }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "\n", "rng = np.random.default_rng()\n", "s1 = pd.Series(rng.normal(size=5))\n", "s2 = pd.Series(rng.normal(size=7))" ] }, { "cell_type": "markdown", "id": "35204d7d", "metadata": {}, "source": [ "Addiert man diese Werte, erhält man:" ] }, { "cell_type": "code", "execution_count": 2, "id": "9c5ca9ed", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:19:50.775547Z", "iopub.status.busy": "2026-05-21T16:19:50.775340Z", "iopub.status.idle": "2026-05-21T16:19:50.780365Z", "shell.execute_reply": "2026-05-21T16:19:50.780147Z", "shell.execute_reply.started": "2026-05-21T16:19:50.775535Z" } }, "outputs": [ { "data": { "text/plain": [ "0 0.722551\n", "1 -2.406030\n", "2 -0.890058\n", "3 0.567861\n", "4 -0.436258\n", "5 NaN\n", "6 NaN\n", "dtype: float64" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 + s2" ] }, { "cell_type": "markdown", "id": "658fc144", "metadata": {}, "source": [ "Der interne Datenabgleich führt zu fehlenden Werten an den Stellen der Labels, die sich nicht überschneiden. Fehlende Werte werden dann bei weiteren arithmetischen Berechnungen weitergegeben." ] }, { "cell_type": "markdown", "id": "9a8a4ca0", "metadata": {}, "source": [ "Bei DataFrames wird die Ausrichtung sowohl für die Zeilen als auch für die Spalten durchgeführt:" ] }, { "cell_type": "code", "execution_count": 3, "id": "02308a89", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:19:50.780762Z", "iopub.status.busy": "2026-05-21T16:19:50.780674Z", "iopub.status.idle": "2026-05-21T16:19:50.783758Z", "shell.execute_reply": "2026-05-21T16:19:50.783136Z", "shell.execute_reply.started": "2026-05-21T16:19:50.780755Z" } }, "outputs": [], "source": [ "df1 = pd.DataFrame(rng.normal(size=(5, 3)))\n", "df2 = pd.DataFrame(rng.normal(size=(7, 2)))" ] }, { "cell_type": "markdown", "id": "5b6810dd", "metadata": {}, "source": [ "Wenn die beiden DataFrames addiert werden, ergibt sich ein DataFrame, dessen Index und Spalten die Vereinigungen derjenigen in jedem der obigen DataFrames sind:" ] }, { "cell_type": "code", "execution_count": 4, "id": "dd2da918", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:19:50.784149Z", "iopub.status.busy": "2026-05-21T16:19:50.784059Z", "iopub.status.idle": "2026-05-21T16:19:50.789641Z", "shell.execute_reply": "2026-05-21T16:19:50.789411Z", "shell.execute_reply.started": "2026-05-21T16:19:50.784141Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012
00.273899-0.568106NaN
1-0.337689-1.422105NaN
21.3792090.996964NaN
3-0.398303-0.325736NaN
40.1430511.320471NaN
5NaNNaNNaN
6NaNNaNNaN
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 0.273899 -0.568106 NaN\n", "1 -0.337689 -1.422105 NaN\n", "2 1.379209 0.996964 NaN\n", "3 -0.398303 -0.325736 NaN\n", "4 0.143051 1.320471 NaN\n", "5 NaN NaN NaN\n", "6 NaN NaN NaN" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df1 + df2" ] }, { "cell_type": "markdown", "id": "635e754e", "metadata": {}, "source": [ "Da die Spalte 2 nicht in beiden DataFrame-Objekten vorkommen, erscheinen sie im Ergebnis als fehlend. Das Gleiche gilt für die Zeilen, deren Bezeichnungen nicht in beiden Objekten vorkommen." ] }, { "cell_type": "markdown", "id": "4a99f089", "metadata": {}, "source": [ "## Arithmetische Methoden mit Füllwerten\n", "\n", "Bei arithmetischen Operationen zwischen unterschiedlich indizierten Objekten kann es sinnvoll sein, einen speziellen Wert (z. B. `0`) zu verwenden, wenn eine Achsenbeschriftung in einem Objekt gefunden wird, im anderen aber nicht. Mit der `add`-Methode kann das Argument `fill_value` übergeben werden:" ] }, { "cell_type": "code", "execution_count": 5, "id": "b587fa86", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:19:50.790431Z", "iopub.status.busy": "2026-05-21T16:19:50.790137Z", "iopub.status.idle": "2026-05-21T16:19:50.794529Z", "shell.execute_reply": "2026-05-21T16:19:50.794216Z", "shell.execute_reply.started": "2026-05-21T16:19:50.790420Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012
00.273899-0.5681060.650960
1-0.337689-1.4221050.973974
21.3792090.9969640.556086
3-0.398303-0.325736-0.283546
40.1430511.3204711.307053
50.201034-1.587605NaN
60.747488-0.389178NaN
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 0.273899 -0.568106 0.650960\n", "1 -0.337689 -1.422105 0.973974\n", "2 1.379209 0.996964 0.556086\n", "3 -0.398303 -0.325736 -0.283546\n", "4 0.143051 1.320471 1.307053\n", "5 0.201034 -1.587605 NaN\n", "6 0.747488 -0.389178 NaN" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df12 = df1.add(df2, fill_value=0)\n", "\n", "df12" ] }, { "cell_type": "markdown", "id": "3184db4a", "metadata": {}, "source": [ "Im folgenden Beispiel setzen wir die beiden verbleibenden NaN-Werte auf `0`:" ] }, { "cell_type": "code", "execution_count": 6, "id": "a91d7a81", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:19:50.796214Z", "iopub.status.busy": "2026-05-21T16:19:50.796073Z", "iopub.status.idle": "2026-05-21T16:19:50.797913Z", "shell.execute_reply": "2026-05-21T16:19:50.797681Z", "shell.execute_reply.started": "2026-05-21T16:19:50.796200Z" } }, "outputs": [], "source": [ "df12.iloc[[5, 6], [2]] = 0" ] }, { "cell_type": "code", "execution_count": 7, "id": "0467d4bc", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:19:50.798593Z", "iopub.status.busy": "2026-05-21T16:19:50.798396Z", "iopub.status.idle": "2026-05-21T16:19:50.802239Z", "shell.execute_reply": "2026-05-21T16:19:50.801942Z", "shell.execute_reply.started": "2026-05-21T16:19:50.798584Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012
00.273899-0.5681060.650960
1-0.337689-1.4221050.973974
21.3792090.9969640.556086
3-0.398303-0.325736-0.283546
40.1430511.3204711.307053
50.201034-1.5876050.000000
60.747488-0.3891780.000000
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 0.273899 -0.568106 0.650960\n", "1 -0.337689 -1.422105 0.973974\n", "2 1.379209 0.996964 0.556086\n", "3 -0.398303 -0.325736 -0.283546\n", "4 0.143051 1.320471 1.307053\n", "5 0.201034 -1.587605 0.000000\n", "6 0.747488 -0.389178 0.000000" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df12" ] }, { "cell_type": "markdown", "id": "75266ce2", "metadata": {}, "source": [ "## Arithmetische Methoden\n", "\n", "Methode | Beschreibung\n", ":------ | :-----------\n", "`add`, `radd` | Methoden für Addition (`+`)\n", "`sub`, `rsub` | Methoden für die Subtraktion (`-`)\n", "`div`, `rdiv` | Methoden für die Division (`/`)\n", "`floordiv`, `rfloordiv` | Methoden für die Abrundungsfunktion (engl.: floor divison) (`//`)\n", "`mul`, `rmul` | Methoden für die Multiplikation (`*`)\n", "`pow`, `rpow` | Methoden zur Potenzierung (`**`)\n", "\n", "`r` (engl.: _reverse_) kehrt die Methode um." ] }, { "cell_type": "markdown", "id": "72c03854", "metadata": {}, "source": [ "## Operationen zwischen DataFrame und Series\n", "\n", "Wie bei NumPy-Arrays verschiedener Dimensionen ist auch die Arithmetik zwischen DataFrame und Series definiert." ] }, { "cell_type": "code", "execution_count": 8, "id": "8d7b47b5", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:19:50.802650Z", "iopub.status.busy": "2026-05-21T16:19:50.802585Z", "iopub.status.idle": "2026-05-21T16:19:50.806262Z", "shell.execute_reply": "2026-05-21T16:19:50.805992Z", "shell.execute_reply.started": "2026-05-21T16:19:50.802643Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
01234
00.028700-1.1081550.334155NaNNaN
1-0.582888-1.9621540.657169NaNNaN
21.1340100.4569160.239281NaNNaN
3-0.643501-0.865785-0.600351NaNNaN
4-0.1021470.7804230.990248NaNNaN
5-0.044165-2.127653-0.316805NaNNaN
60.502290-0.929227-0.316805NaNNaN
\n", "
" ], "text/plain": [ " 0 1 2 3 4\n", "0 0.028700 -1.108155 0.334155 NaN NaN\n", "1 -0.582888 -1.962154 0.657169 NaN NaN\n", "2 1.134010 0.456916 0.239281 NaN NaN\n", "3 -0.643501 -0.865785 -0.600351 NaN NaN\n", "4 -0.102147 0.780423 0.990248 NaN NaN\n", "5 -0.044165 -2.127653 -0.316805 NaN NaN\n", "6 0.502290 -0.929227 -0.316805 NaN NaN" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 + df12" ] }, { "cell_type": "markdown", "id": "3048aa55", "metadata": {}, "source": [ "Wenn wir `s1` mit `df12` addieren, wird die Additon für jede Zeile einmal durchgeführt. Dies wird als _Broadcasting_ bezeichnet. Standardmäßig entspricht die Arithmetik zwischen DataFrame und Serie dem Index der Serie in den Spalten des DataFrame, wobei die Zeilen nach unten übertragen werden." ] }, { "cell_type": "markdown", "id": "3d69cc0e", "metadata": {}, "source": [ "Wenn ein Indexwert weder in den Spalten des DataFrame noch im Index der Serie gefunden wird, werden die Objekte neu indiziert, um die Vereinigung zu bilden:" ] }, { "cell_type": "markdown", "id": "c252358c", "metadata": {}, "source": [ "Wenn ihr stattdessen die Spalten übertragen und die Zeilen abgleichen wollt, müsst ihr eine der arithmetischen Methoden verwenden, z.B.:" ] }, { "cell_type": "code", "execution_count": 9, "id": "b5499888", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:19:50.806741Z", "iopub.status.busy": "2026-05-21T16:19:50.806661Z", "iopub.status.idle": "2026-05-21T16:19:50.811067Z", "shell.execute_reply": "2026-05-21T16:19:50.810788Z", "shell.execute_reply.started": "2026-05-21T16:19:50.806733Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012
01.2416480.3996431.618709
1-2.203672-3.288087-0.892008
20.8059560.423711-0.017167
3-0.886309-0.813743-0.771553
40.0044281.1818481.168430
50.183869-1.604769-0.017164
60.322119-0.814548-0.425370
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 1.241648 0.399643 1.618709\n", "1 -2.203672 -3.288087 -0.892008\n", "2 0.805956 0.423711 -0.017167\n", "3 -0.886309 -0.813743 -0.771553\n", "4 0.004428 1.181848 1.168430\n", "5 0.183869 -1.604769 -0.017164\n", "6 0.322119 -0.814548 -0.425370" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df12.add(s2, axis=\"index\")" ] }, { "cell_type": "markdown", "id": "f4dabeca", "metadata": {}, "source": [ "Die Achsennummer, die ihr übergebt, ist die Achse, auf die abgeglichen werden soll. In diesem Fall soll der Zeilenindex des DataFrame (`axis='index'` oder `axis=0`) abgeglichen und übertragen werden." ] }, { "cell_type": "markdown", "id": "f04a8ce0", "metadata": {}, "source": [ "## Funktionsanwendung und Mapping\n", "\n", "`numpy.ufunc` (elementweise Array-Methoden) funktionieren auch mit Pandas-Objekten:" ] }, { "cell_type": "code", "execution_count": 10, "id": "6703726d", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:19:50.811450Z", "iopub.status.busy": "2026-05-21T16:19:50.811367Z", "iopub.status.idle": "2026-05-21T16:19:50.814553Z", "shell.execute_reply": "2026-05-21T16:19:50.814357Z", "shell.execute_reply.started": "2026-05-21T16:19:50.811443Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012
00.2738990.5681060.650960
10.3376891.4221050.973974
21.3792090.9969640.556086
30.3983030.3257360.283546
40.1430511.3204711.307053
50.2010341.5876050.000000
60.7474880.3891780.000000
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 0.273899 0.568106 0.650960\n", "1 0.337689 1.422105 0.973974\n", "2 1.379209 0.996964 0.556086\n", "3 0.398303 0.325736 0.283546\n", "4 0.143051 1.320471 1.307053\n", "5 0.201034 1.587605 0.000000\n", "6 0.747488 0.389178 0.000000" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.abs(df12)" ] }, { "cell_type": "markdown", "id": "e6cb5cbe", "metadata": {}, "source": [ "Eine weitere häufige Operation ist die Anwendung einer Funktion auf eindimensionale Arrays auf jede Spalte oder Zeile. Die [pandas.DataFrame.apply](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html)-Methode tut genau dies:" ] }, { "cell_type": "code", "execution_count": 11, "id": "fba73255", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:19:50.814942Z", "iopub.status.busy": "2026-05-21T16:19:50.814849Z", "iopub.status.idle": "2026-05-21T16:19:50.818491Z", "shell.execute_reply": "2026-05-21T16:19:50.818221Z", "shell.execute_reply.started": "2026-05-21T16:19:50.814936Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012
00.273899-0.5681060.650960
1-0.337689-1.4221050.973974
21.3792090.9969640.556086
3-0.398303-0.325736-0.283546
40.1430511.3204711.307053
50.201034-1.5876050.000000
60.747488-0.3891780.000000
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 0.273899 -0.568106 0.650960\n", "1 -0.337689 -1.422105 0.973974\n", "2 1.379209 0.996964 0.556086\n", "3 -0.398303 -0.325736 -0.283546\n", "4 0.143051 1.320471 1.307053\n", "5 0.201034 -1.587605 0.000000\n", "6 0.747488 -0.389178 0.000000" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df12" ] }, { "cell_type": "code", "execution_count": 12, "id": "e7589502", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:19:50.819066Z", "iopub.status.busy": "2026-05-21T16:19:50.818948Z", "iopub.status.idle": "2026-05-21T16:19:50.821686Z", "shell.execute_reply": "2026-05-21T16:19:50.821478Z", "shell.execute_reply.started": "2026-05-21T16:19:50.819058Z" } }, "outputs": [ { "data": { "text/plain": [ "0 1.777512\n", "1 2.908076\n", "2 1.590599\n", "dtype: float64" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def minmaxrange(x):\n", " return x.max() - x.min()\n", "\n", "\n", "df12.apply(minmaxrange)" ] }, { "cell_type": "markdown", "id": "5c730fc0", "metadata": {}, "source": [ "Hier wird die Funktion `f`, die die Differenz zwischen dem Maximum und dem Minimum einer Reihe berechnet, einmal für jede Spalte des Rahmens aufgerufen. Das Ergebnis ist eine Reihe mit den Spalten des Rahmens als Index." ] }, { "cell_type": "markdown", "id": "cd3d8cef", "metadata": {}, "source": [ "Wenn ihr `axis='columns'` an `apply` übergebt, wird die Funktion stattdessen einmal pro Zeile aufgerufen:" ] }, { "cell_type": "code", "execution_count": 13, "id": "8db58697", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:19:50.822154Z", "iopub.status.busy": "2026-05-21T16:19:50.822077Z", "iopub.status.idle": "2026-05-21T16:19:50.825278Z", "shell.execute_reply": "2026-05-21T16:19:50.824668Z", "shell.execute_reply.started": "2026-05-21T16:19:50.822146Z" } }, "outputs": [ { "data": { "text/plain": [ "0 1.219066\n", "1 2.396079\n", "2 0.823123\n", "3 0.114757\n", "4 1.177420\n", "5 1.788638\n", "6 1.136666\n", "dtype: float64" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df12.apply(minmaxrange, axis=\"columns\")" ] }, { "cell_type": "markdown", "id": "1dfebb36", "metadata": {}, "source": [ "Viele der gebräuchlichsten Array-Statistiken (wie `sum` und `mean`) sind DataFrame-Methoden, so dass die Verwendung von `apply` nicht notwendig ist." ] }, { "cell_type": "markdown", "id": "429fb65c", "metadata": {}, "source": [ "Die an apply übergebene Funktion muss keinen Einzelwert zurückgeben; sie kann auch eine Reihe mit mehreren Werten zurückgeben:" ] }, { "cell_type": "code", "execution_count": 14, "id": "758ab932", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:19:50.826185Z", "iopub.status.busy": "2026-05-21T16:19:50.825993Z", "iopub.status.idle": "2026-05-21T16:19:50.831121Z", "shell.execute_reply": "2026-05-21T16:19:50.830808Z", "shell.execute_reply.started": "2026-05-21T16:19:50.826175Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012
min-0.398303-1.587605-0.283546
max1.3792091.3204711.307053
\n", "
" ], "text/plain": [ " 0 1 2\n", "min -0.398303 -1.587605 -0.283546\n", "max 1.379209 1.320471 1.307053" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def f(x):\n", " return pd.Series([x.min(), x.max()], index=[\"min\", \"max\"])\n", "\n", "\n", "df12.apply(f)" ] }, { "cell_type": "markdown", "id": "5b6f33bc", "metadata": {}, "source": [ "Es können auch elementweise Python-Funktionen verwendet werden. Angenommen, ihr möchtet aus jedem Fließkommawert in `df12` eine formatierte Zeichenkette berechnen. Dies könnt ihr mit [pandas.DataFrame.applymap](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.applymap.html) erreichen:" ] }, { "cell_type": "code", "execution_count": 15, "id": "ba3f9ec6", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:19:50.831557Z", "iopub.status.busy": "2026-05-21T16:19:50.831432Z", "iopub.status.idle": "2026-05-21T16:19:50.836051Z", "shell.execute_reply": "2026-05-21T16:19:50.835722Z", "shell.execute_reply.started": "2026-05-21T16:19:50.831550Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012
00.27-0.570.65
1-0.34-1.420.97
21.381.000.56
3-0.40-0.33-0.28
40.141.321.31
50.20-1.590.00
60.75-0.390.00
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 0.27 -0.57 0.65\n", "1 -0.34 -1.42 0.97\n", "2 1.38 1.00 0.56\n", "3 -0.40 -0.33 -0.28\n", "4 0.14 1.32 1.31\n", "5 0.20 -1.59 0.00\n", "6 0.75 -0.39 0.00" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def round_two(x):\n", " return round(x, 2)\n", "\n", "\n", "df12.map(round_two)" ] }, { "cell_type": "markdown", "id": "cda74d52", "metadata": {}, "source": [ "Der Grund für den Namen `applymap` ist, dass Series über eine `map`-Methode zur Anwendung einer elementweisen Funktion verfügt:" ] }, { "cell_type": "code", "execution_count": 16, "id": "df04727d", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:19:50.836674Z", "iopub.status.busy": "2026-05-21T16:19:50.836544Z", "iopub.status.idle": "2026-05-21T16:19:50.839394Z", "shell.execute_reply": "2026-05-21T16:19:50.839109Z", "shell.execute_reply.started": "2026-05-21T16:19:50.836663Z" } }, "outputs": [ { "data": { "text/plain": [ "0 0.65\n", "1 0.97\n", "2 0.56\n", "3 -0.28\n", "4 1.31\n", "5 0.00\n", "6 0.00\n", "Name: 2, dtype: float64" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df12[2].map(round_two)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.13 Kernel", "language": "python", "name": "python313" }, "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.13.0" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }