{
"cells": [
{
"cell_type": "markdown",
"id": "b95ac834",
"metadata": {},
"source": [
"# Selecting and filtering data\n",
"\n",
"Indexing series `(obj[...])` works similarly to indexing NumPy arrays, except that you can use index values of the series instead of just integers. Here are some examples:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "3482a24f",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:01.101903Z",
"iopub.status.busy": "2026-05-17T14:31:01.101654Z",
"iopub.status.idle": "2026-05-17T14:31:01.321903Z",
"shell.execute_reply": "2026-05-17T14:31:01.321588Z",
"shell.execute_reply.started": "2026-05-17T14:31:01.101878Z"
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f4edf9bc",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:01.322438Z",
"iopub.status.busy": "2026-05-17T14:31:01.322314Z",
"iopub.status.idle": "2026-05-17T14:31:01.325499Z",
"shell.execute_reply": "2026-05-17T14:31:01.325086Z",
"shell.execute_reply.started": "2026-05-17T14:31:01.322430Z"
}
},
"outputs": [],
"source": [
"idx = pd.date_range(\"2022-02-02\", periods=7)\n",
"rng = np.random.default_rng()\n",
"s = pd.Series(rng.random(7), index=idx)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "237188da",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:01.326037Z",
"iopub.status.busy": "2026-05-17T14:31:01.325959Z",
"iopub.status.idle": "2026-05-17T14:31:01.329864Z",
"shell.execute_reply": "2026-05-17T14:31:01.329643Z",
"shell.execute_reply.started": "2026-05-17T14:31:01.326030Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2022-02-02 0.472935\n",
"2022-02-03 0.872472\n",
"2022-02-04 0.875741\n",
"2022-02-05 0.348128\n",
"2022-02-06 0.713738\n",
"2022-02-07 0.119012\n",
"2022-02-08 0.509543\n",
"Freq: D, dtype: float64"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "1b12fbaa",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:01.330457Z",
"iopub.status.busy": "2026-05-17T14:31:01.330337Z",
"iopub.status.idle": "2026-05-17T14:31:01.332803Z",
"shell.execute_reply": "2026-05-17T14:31:01.332591Z",
"shell.execute_reply.started": "2026-05-17T14:31:01.330448Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"np.float64(0.8724721164207767)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[\"2022-02-03\"]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "7da4fd93",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:01.333294Z",
"iopub.status.busy": "2026-05-17T14:31:01.333218Z",
"iopub.status.idle": "2026-05-17T14:31:01.335660Z",
"shell.execute_reply": "2026-05-17T14:31:01.335358Z",
"shell.execute_reply.started": "2026-05-17T14:31:01.333288Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"np.float64(0.8724721164207767)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.iloc[1]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "9531e948",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:01.337407Z",
"iopub.status.busy": "2026-05-17T14:31:01.337328Z",
"iopub.status.idle": "2026-05-17T14:31:01.339978Z",
"shell.execute_reply": "2026-05-17T14:31:01.339709Z",
"shell.execute_reply.started": "2026-05-17T14:31:01.337400Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2022-02-04 0.875741\n",
"2022-02-05 0.348128\n",
"Freq: D, dtype: float64"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[2:4]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c3b587c0",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:01.340471Z",
"iopub.status.busy": "2026-05-17T14:31:01.340396Z",
"iopub.status.idle": "2026-05-17T14:31:01.343582Z",
"shell.execute_reply": "2026-05-17T14:31:01.343307Z",
"shell.execute_reply.started": "2026-05-17T14:31:01.340464Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2022-02-04 0.875741\n",
"2022-02-03 0.872472\n",
"2022-02-02 0.472935\n",
"dtype: float64"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[[\"2022-02-04\", \"2022-02-03\", \"2022-02-02\"]]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "31df8ae7",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:01.344172Z",
"iopub.status.busy": "2026-05-17T14:31:01.344077Z",
"iopub.status.idle": "2026-05-17T14:31:01.347122Z",
"shell.execute_reply": "2026-05-17T14:31:01.346912Z",
"shell.execute_reply.started": "2026-05-17T14:31:01.344164Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2022-02-03 0.872472\n",
"2022-02-05 0.348128\n",
"Freq: 2D, dtype: float64"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.iloc[[1, 3]]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "4d612a38-6996-4e8d-ab5d-7b125ae66099",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:01.347602Z",
"iopub.status.busy": "2026-05-17T14:31:01.347498Z",
"iopub.status.idle": "2026-05-17T14:31:01.351189Z",
"shell.execute_reply": "2026-05-17T14:31:01.350887Z",
"shell.execute_reply.started": "2026-05-17T14:31:01.347594Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2022-02-02 0.472935\n",
"2022-02-03 0.872472\n",
"2022-02-04 0.875741\n",
"2022-02-05 0.348128\n",
"2022-02-06 0.713738\n",
"2022-02-07 0.119012\n",
"2022-02-08 0.509543\n",
"Freq: D, dtype: float64"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[s > 0]"
]
},
{
"cell_type": "markdown",
"id": "82b5ebef",
"metadata": {},
"source": [
"Although this allows you to select data by label, the preferred method for selecting index values is the `loc` operator:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "0e11f024",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:01.351633Z",
"iopub.status.busy": "2026-05-17T14:31:01.351536Z",
"iopub.status.idle": "2026-05-17T14:31:01.354767Z",
"shell.execute_reply": "2026-05-17T14:31:01.354478Z",
"shell.execute_reply.started": "2026-05-17T14:31:01.351625Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2022-02-04 0.875741\n",
"2022-02-03 0.872472\n",
"2022-02-02 0.472935\n",
"dtype: float64"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.loc[[\"2022-02-04\", \"2022-02-03\", \"2022-02-02\"]]"
]
},
{
"cell_type": "markdown",
"id": "7b3b6f16",
"metadata": {},
"source": [
"The reason for preferring `loc` lies in the different treatment of integers when indexing with `[]`. In regular `[]` based indexing, integers are treated as labels if the index contains integers, so the behaviour differs depending on the data type of the index. In our example, the expression `s.loc[[3, 2, 1]]` will fail because the index does not contain integers:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "c7279faf",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:01.355244Z",
"iopub.status.busy": "2026-05-17T14:31:01.355174Z",
"iopub.status.idle": "2026-05-17T14:31:01.579420Z",
"shell.execute_reply": "2026-05-17T14:31:01.578482Z",
"shell.execute_reply.started": "2026-05-17T14:31:01.355237Z"
}
},
"outputs": [
{
"ename": "KeyError",
"evalue": "\"None of [Index([3, 2, 1], dtype='int64')] are in the [index]\"",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[11], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43ms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mloc\u001b[49m\u001b[43m[\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m]\u001b[49m\n",
"File \u001b[0;32m~/cusy/trn/jupyter-tutorial/uvenvs/py313/.venv/lib/python3.13/site-packages/pandas/core/indexing.py:1191\u001b[0m, in \u001b[0;36m_LocationIndexer.__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 1189\u001b[0m maybe_callable \u001b[38;5;241m=\u001b[39m com\u001b[38;5;241m.\u001b[39mapply_if_callable(key, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj)\n\u001b[1;32m 1190\u001b[0m maybe_callable \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_check_deprecated_callable_usage(key, maybe_callable)\n\u001b[0;32m-> 1191\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_getitem_axis\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmaybe_callable\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/cusy/trn/jupyter-tutorial/uvenvs/py313/.venv/lib/python3.13/site-packages/pandas/core/indexing.py:1420\u001b[0m, in \u001b[0;36m_LocIndexer._getitem_axis\u001b[0;34m(self, key, axis)\u001b[0m\n\u001b[1;32m 1417\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(key, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mndim\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m key\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[1;32m 1418\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCannot index with multidimensional key\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m-> 1420\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_getitem_iterable\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1422\u001b[0m \u001b[38;5;66;03m# nested tuple slicing\u001b[39;00m\n\u001b[1;32m 1423\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_nested_tuple(key, labels):\n",
"File \u001b[0;32m~/cusy/trn/jupyter-tutorial/uvenvs/py313/.venv/lib/python3.13/site-packages/pandas/core/indexing.py:1360\u001b[0m, in \u001b[0;36m_LocIndexer._getitem_iterable\u001b[0;34m(self, key, axis)\u001b[0m\n\u001b[1;32m 1357\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_key(key, axis)\n\u001b[1;32m 1359\u001b[0m \u001b[38;5;66;03m# A collection of keys\u001b[39;00m\n\u001b[0;32m-> 1360\u001b[0m keyarr, indexer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_listlike_indexer\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1361\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39m_reindex_with_indexers(\n\u001b[1;32m 1362\u001b[0m {axis: [keyarr, indexer]}, copy\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, allow_dups\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m 1363\u001b[0m )\n",
"File \u001b[0;32m~/cusy/trn/jupyter-tutorial/uvenvs/py313/.venv/lib/python3.13/site-packages/pandas/core/indexing.py:1558\u001b[0m, in \u001b[0;36m_LocIndexer._get_listlike_indexer\u001b[0;34m(self, key, axis)\u001b[0m\n\u001b[1;32m 1555\u001b[0m ax \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39m_get_axis(axis)\n\u001b[1;32m 1556\u001b[0m axis_name \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39m_get_axis_name(axis)\n\u001b[0;32m-> 1558\u001b[0m keyarr, indexer \u001b[38;5;241m=\u001b[39m \u001b[43max\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_indexer_strict\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1560\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m keyarr, indexer\n",
"File \u001b[0;32m~/cusy/trn/jupyter-tutorial/uvenvs/py313/.venv/lib/python3.13/site-packages/pandas/core/indexes/base.py:6200\u001b[0m, in \u001b[0;36mIndex._get_indexer_strict\u001b[0;34m(self, key, axis_name)\u001b[0m\n\u001b[1;32m 6197\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 6198\u001b[0m keyarr, indexer, new_indexer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_reindex_non_unique(keyarr)\n\u001b[0;32m-> 6200\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_raise_if_missing\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkeyarr\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mindexer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 6202\u001b[0m keyarr \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtake(indexer)\n\u001b[1;32m 6203\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(key, Index):\n\u001b[1;32m 6204\u001b[0m \u001b[38;5;66;03m# GH 42790 - Preserve name from an Index\u001b[39;00m\n",
"File \u001b[0;32m~/cusy/trn/jupyter-tutorial/uvenvs/py313/.venv/lib/python3.13/site-packages/pandas/core/indexes/base.py:6249\u001b[0m, in \u001b[0;36mIndex._raise_if_missing\u001b[0;34m(self, key, indexer, axis_name)\u001b[0m\n\u001b[1;32m 6247\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m nmissing:\n\u001b[1;32m 6248\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m nmissing \u001b[38;5;241m==\u001b[39m \u001b[38;5;28mlen\u001b[39m(indexer):\n\u001b[0;32m-> 6249\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNone of [\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mkey\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m] are in the [\u001b[39m\u001b[38;5;132;01m{\u001b[39;00maxis_name\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m]\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 6251\u001b[0m not_found \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlist\u001b[39m(ensure_index(key)[missing_mask\u001b[38;5;241m.\u001b[39mnonzero()[\u001b[38;5;241m0\u001b[39m]]\u001b[38;5;241m.\u001b[39munique())\n\u001b[1;32m 6252\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mnot_found\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m not in index\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
"\u001b[0;31mKeyError\u001b[0m: \"None of [Index([3, 2, 1], dtype='int64')] are in the [index]\""
]
}
],
"source": [
"s.loc[[3, 2, 1]]"
]
},
{
"cell_type": "markdown",
"id": "ca01bbcf",
"metadata": {},
"source": [
"Während der `loc`-Operator ausschließlich Label indiziert, indiziert der `iloc`-Operator ausschließlich mit ganzen Zahlen:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "37e0ba74",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:21.337607Z",
"iopub.status.busy": "2026-05-17T14:31:21.337313Z",
"iopub.status.idle": "2026-05-17T14:31:21.342269Z",
"shell.execute_reply": "2026-05-17T14:31:21.341792Z",
"shell.execute_reply.started": "2026-05-17T14:31:21.337589Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2022-02-05 0.348128\n",
"2022-02-04 0.875741\n",
"2022-02-03 0.872472\n",
"Freq: -1D, dtype: float64"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.iloc[[3, 2, 1]]"
]
},
{
"cell_type": "markdown",
"id": "e5dbc895",
"metadata": {},
"source": [
"Ihr könnt auch mit Labels slicen, aber das funktioniert anders als das normale Python-Slicing, da der Endpunkt inklusive ist:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "2121259e",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:22.554652Z",
"iopub.status.busy": "2026-05-17T14:31:22.553987Z",
"iopub.status.idle": "2026-05-17T14:31:22.563125Z",
"shell.execute_reply": "2026-05-17T14:31:22.562717Z",
"shell.execute_reply.started": "2026-05-17T14:31:22.554600Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2022-02-03 0.872472\n",
"2022-02-04 0.875741\n",
"Freq: D, dtype: float64"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.loc[\"2022-02-03\":\"2022-02-04\"]"
]
},
{
"cell_type": "markdown",
"id": "3ebe649f",
"metadata": {},
"source": [
"Durch die Einstellung mit diesen Methoden wird der entsprechende Abschnitt der Reihe geändert:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "4af5a091",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:23.365949Z",
"iopub.status.busy": "2026-05-17T14:31:23.365617Z",
"iopub.status.idle": "2026-05-17T14:31:23.371453Z",
"shell.execute_reply": "2026-05-17T14:31:23.371041Z",
"shell.execute_reply.started": "2026-05-17T14:31:23.365927Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2022-02-02 0.472935\n",
"2022-02-03 0.000000\n",
"2022-02-04 0.000000\n",
"2022-02-05 0.348128\n",
"2022-02-06 0.713738\n",
"2022-02-07 0.119012\n",
"2022-02-08 0.509543\n",
"Freq: D, dtype: float64"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.loc[\"2022-02-03\":\"2022-02-04\"] = 0\n",
"\n",
"s"
]
},
{
"cell_type": "markdown",
"id": "2cdf7f54",
"metadata": {},
"source": [
"Die Indizierung in einem DataFrame dient dazu, eine oder mehrere Spalten entweder mit einem einzelnen Wert oder einer Folge abzurufen:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "d2ac569b",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:24.259642Z",
"iopub.status.busy": "2026-05-17T14:31:24.258960Z",
"iopub.status.idle": "2026-05-17T14:31:24.273094Z",
"shell.execute_reply": "2026-05-17T14:31:24.272379Z",
"shell.execute_reply.started": "2026-05-17T14:31:24.259598Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Decimal | \n",
" Octal | \n",
" Key | \n",
"
\n",
" \n",
" | Code | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | U+0000 | \n",
" 0 | \n",
" 001 | \n",
" NUL | \n",
"
\n",
" \n",
" | U+0001 | \n",
" 1 | \n",
" 002 | \n",
" Ctrl-A | \n",
"
\n",
" \n",
" | U+0002 | \n",
" 2 | \n",
" 003 | \n",
" Ctrl-B | \n",
"
\n",
" \n",
" | U+0003 | \n",
" 3 | \n",
" 004 | \n",
" Ctrl-C | \n",
"
\n",
" \n",
" | U+0004 | \n",
" 4 | \n",
" 004 | \n",
" Ctrl-D | \n",
"
\n",
" \n",
" | U+0005 | \n",
" 5 | \n",
" 005 | \n",
" Ctrl-E | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Decimal Octal Key\n",
"Code \n",
"U+0000 0 001 NUL\n",
"U+0001 1 002 Ctrl-A\n",
"U+0002 2 003 Ctrl-B\n",
"U+0003 3 004 Ctrl-C\n",
"U+0004 4 004 Ctrl-D\n",
"U+0005 5 005 Ctrl-E"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = {\n",
" \"Code\": [\"U+0000\", \"U+0001\", \"U+0002\", \"U+0003\", \"U+0004\", \"U+0005\"],\n",
" \"Decimal\": [0, 1, 2, 3, 4, 5],\n",
" \"Octal\": [\"001\", \"002\", \"003\", \"004\", \"004\", \"005\"],\n",
" \"Key\": [\"NUL\", \"Ctrl-A\", \"Ctrl-B\", \"Ctrl-C\", \"Ctrl-D\", \"Ctrl-E\"],\n",
"}\n",
"\n",
"df = pd.DataFrame(data)\n",
"df = pd.DataFrame(data, columns=[\"Decimal\", \"Octal\", \"Key\"], index=df[\"Code\"])\n",
"\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "81c7bce1",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:24.563667Z",
"iopub.status.busy": "2026-05-17T14:31:24.562995Z",
"iopub.status.idle": "2026-05-17T14:31:24.571053Z",
"shell.execute_reply": "2026-05-17T14:31:24.570242Z",
"shell.execute_reply.started": "2026-05-17T14:31:24.563619Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Code\n",
"U+0000 NUL\n",
"U+0001 Ctrl-A\n",
"U+0002 Ctrl-B\n",
"U+0003 Ctrl-C\n",
"U+0004 Ctrl-D\n",
"U+0005 Ctrl-E\n",
"Name: Key, dtype: object"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[\"Key\"]"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "923b8440",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:24.890850Z",
"iopub.status.busy": "2026-05-17T14:31:24.890226Z",
"iopub.status.idle": "2026-05-17T14:31:24.901389Z",
"shell.execute_reply": "2026-05-17T14:31:24.900739Z",
"shell.execute_reply.started": "2026-05-17T14:31:24.890801Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Decimal | \n",
" Key | \n",
"
\n",
" \n",
" | Code | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | U+0000 | \n",
" 0 | \n",
" NUL | \n",
"
\n",
" \n",
" | U+0001 | \n",
" 1 | \n",
" Ctrl-A | \n",
"
\n",
" \n",
" | U+0002 | \n",
" 2 | \n",
" Ctrl-B | \n",
"
\n",
" \n",
" | U+0003 | \n",
" 3 | \n",
" Ctrl-C | \n",
"
\n",
" \n",
" | U+0004 | \n",
" 4 | \n",
" Ctrl-D | \n",
"
\n",
" \n",
" | U+0005 | \n",
" 5 | \n",
" Ctrl-E | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Decimal Key\n",
"Code \n",
"U+0000 0 NUL\n",
"U+0001 1 Ctrl-A\n",
"U+0002 2 Ctrl-B\n",
"U+0003 3 Ctrl-C\n",
"U+0004 4 Ctrl-D\n",
"U+0005 5 Ctrl-E"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[[\"Decimal\", \"Key\"]]"
]
},
{
"cell_type": "markdown",
"id": "b90336c6",
"metadata": {},
"source": [
"Die Zeilenauswahlsyntax `df[:2]` wird aus Gründen der Bequemlichkeit bereitgestellt. Durch die Übergabe eines einzelnen Elements oder einer Liste an den `[]`-Operator werden Spalten ausgewählt."
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "72f5a3d0",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:25.798271Z",
"iopub.status.busy": "2026-05-17T14:31:25.797954Z",
"iopub.status.idle": "2026-05-17T14:31:25.806587Z",
"shell.execute_reply": "2026-05-17T14:31:25.805656Z",
"shell.execute_reply.started": "2026-05-17T14:31:25.798250Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Decimal | \n",
" Octal | \n",
" Key | \n",
"
\n",
" \n",
" | Code | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | U+0000 | \n",
" 0 | \n",
" 001 | \n",
" NUL | \n",
"
\n",
" \n",
" | U+0001 | \n",
" 1 | \n",
" 002 | \n",
" Ctrl-A | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Decimal Octal Key\n",
"Code \n",
"U+0000 0 001 NUL\n",
"U+0001 1 002 Ctrl-A"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[:2]"
]
},
{
"cell_type": "markdown",
"id": "95042d47",
"metadata": {},
"source": [
"Ein weiterer Anwendungsfall ist die Indizierung mit einem booleschen DataFrame, der beispielsweise durch einen Skalarvergleich erzeugt wird:"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "edda0660",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:26.351163Z",
"iopub.status.busy": "2026-05-17T14:31:26.350560Z",
"iopub.status.idle": "2026-05-17T14:31:26.355874Z",
"shell.execute_reply": "2026-05-17T14:31:26.355223Z",
"shell.execute_reply.started": "2026-05-17T14:31:26.351122Z"
},
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"Code\n",
"U+0000 False\n",
"U+0001 False\n",
"U+0002 False\n",
"U+0003 True\n",
"U+0004 True\n",
"U+0005 True\n",
"Name: Decimal, dtype: bool"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[\"Decimal\"] > 2"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "dc1ee2c4-58ec-4e35-adb4-37534c042fb4",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:26.646845Z",
"iopub.status.busy": "2026-05-17T14:31:26.646275Z",
"iopub.status.idle": "2026-05-17T14:31:26.653841Z",
"shell.execute_reply": "2026-05-17T14:31:26.653336Z",
"shell.execute_reply.started": "2026-05-17T14:31:26.646804Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Decimal | \n",
" Octal | \n",
" Key | \n",
"
\n",
" \n",
" | Code | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | U+0003 | \n",
" 3 | \n",
" 004 | \n",
" Ctrl-C | \n",
"
\n",
" \n",
" | U+0004 | \n",
" 4 | \n",
" 004 | \n",
" Ctrl-D | \n",
"
\n",
" \n",
" | U+0005 | \n",
" 5 | \n",
" 005 | \n",
" Ctrl-E | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Decimal Octal Key\n",
"Code \n",
"U+0003 3 004 Ctrl-C\n",
"U+0004 4 004 Ctrl-D\n",
"U+0005 5 005 Ctrl-E"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[df[\"Decimal\"] > 2]"
]
},
{
"cell_type": "markdown",
"id": "ade4d89f-b10b-4929-9e8a-3ac3083b4a03",
"metadata": {},
"source": [
"You can also combine these Boolean DataFrames using bitwise operators:"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "eab8dd55-1b70-4f58-beef-6aedc27dfabf",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:27.582349Z",
"iopub.status.busy": "2026-05-17T14:31:27.582022Z",
"iopub.status.idle": "2026-05-17T14:31:27.589114Z",
"shell.execute_reply": "2026-05-17T14:31:27.588617Z",
"shell.execute_reply.started": "2026-05-17T14:31:27.582329Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Decimal | \n",
" Octal | \n",
" Key | \n",
"
\n",
" \n",
" | Code | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | U+0003 | \n",
" 3 | \n",
" 004 | \n",
" Ctrl-C | \n",
"
\n",
" \n",
" | U+0004 | \n",
" 4 | \n",
" 004 | \n",
" Ctrl-D | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Decimal Octal Key\n",
"Code \n",
"U+0003 3 004 Ctrl-C\n",
"U+0004 4 004 Ctrl-D"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[(df[\"Decimal\"] > 2) & (df[\"Decimal\"] < 5)]"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "6f0d97c4-b4b6-44fe-9303-17744ce8d41f",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:27.889508Z",
"iopub.status.busy": "2026-05-17T14:31:27.889229Z",
"iopub.status.idle": "2026-05-17T14:31:27.898262Z",
"shell.execute_reply": "2026-05-17T14:31:27.897461Z",
"shell.execute_reply.started": "2026-05-17T14:31:27.889490Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Decimal | \n",
" Octal | \n",
" Key | \n",
"
\n",
" \n",
" | Code | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | U+0000 | \n",
" 0 | \n",
" 001 | \n",
" NUL | \n",
"
\n",
" \n",
" | U+0001 | \n",
" 1 | \n",
" 002 | \n",
" Ctrl-A | \n",
"
\n",
" \n",
" | U+0002 | \n",
" 2 | \n",
" 003 | \n",
" Ctrl-B | \n",
"
\n",
" \n",
" | U+0005 | \n",
" 5 | \n",
" 005 | \n",
" Ctrl-E | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Decimal Octal Key\n",
"Code \n",
"U+0000 0 001 NUL\n",
"U+0001 1 002 Ctrl-A\n",
"U+0002 2 003 Ctrl-B\n",
"U+0005 5 005 Ctrl-E"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[(df[\"Decimal\"] < 3) | (df[\"Decimal\"] > 4)]"
]
},
{
"cell_type": "markdown",
"id": "0edb24c2",
"metadata": {},
"source": [
"Like Series, DataFrame also has special operators `loc` and `iloc` for label-based and integer indexing, respectively. Since DataFrame is two-dimensional, you can select a subset of rows and columns using NumPy-like notation by using either axis labels (`loc`) or integers (`iloc`)."
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "c8eb86f9",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:29.624913Z",
"iopub.status.busy": "2026-05-17T14:31:29.624328Z",
"iopub.status.idle": "2026-05-17T14:31:29.632188Z",
"shell.execute_reply": "2026-05-17T14:31:29.631708Z",
"shell.execute_reply.started": "2026-05-17T14:31:29.624865Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Decimal 2\n",
"Key Ctrl-B\n",
"Name: U+0002, dtype: object"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[\"U+0002\", [\"Decimal\", \"Key\"]]"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "cc257cbc",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:29.896297Z",
"iopub.status.busy": "2026-05-17T14:31:29.896016Z",
"iopub.status.idle": "2026-05-17T14:31:29.903617Z",
"shell.execute_reply": "2026-05-17T14:31:29.902395Z",
"shell.execute_reply.started": "2026-05-17T14:31:29.896279Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Octal | \n",
" Key | \n",
"
\n",
" \n",
" | Code | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | U+0002 | \n",
" 003 | \n",
" Ctrl-B | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Octal Key\n",
"Code \n",
"U+0002 003 Ctrl-B"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iloc[[2], [1, 2]]"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "04868a17",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:30.251031Z",
"iopub.status.busy": "2026-05-17T14:31:30.250725Z",
"iopub.status.idle": "2026-05-17T14:31:30.260954Z",
"shell.execute_reply": "2026-05-17T14:31:30.260138Z",
"shell.execute_reply.started": "2026-05-17T14:31:30.251012Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Octal | \n",
" Key | \n",
"
\n",
" \n",
" | Code | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | U+0000 | \n",
" 001 | \n",
" NUL | \n",
"
\n",
" \n",
" | U+0001 | \n",
" 002 | \n",
" Ctrl-A | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Octal Key\n",
"Code \n",
"U+0000 001 NUL\n",
"U+0001 002 Ctrl-A"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iloc[[0, 1], [1, 2]]"
]
},
{
"cell_type": "markdown",
"id": "06be621a",
"metadata": {},
"source": [
"Both indexing functions work with slices in addition to individual labels or lists of labels:"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "abb034d7",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:31.271038Z",
"iopub.status.busy": "2026-05-17T14:31:31.270767Z",
"iopub.status.idle": "2026-05-17T14:31:31.275418Z",
"shell.execute_reply": "2026-05-17T14:31:31.274841Z",
"shell.execute_reply.started": "2026-05-17T14:31:31.271020Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Code\n",
"U+0000 NUL\n",
"U+0001 Ctrl-A\n",
"U+0002 Ctrl-B\n",
"U+0003 Ctrl-C\n",
"Name: Key, dtype: object"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[:\"U+0003\", \"Key\"]"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "46ae3947",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-17T14:31:31.970649Z",
"iopub.status.busy": "2026-05-17T14:31:31.970373Z",
"iopub.status.idle": "2026-05-17T14:31:31.979647Z",
"shell.execute_reply": "2026-05-17T14:31:31.978787Z",
"shell.execute_reply.started": "2026-05-17T14:31:31.970631Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Decimal | \n",
" Octal | \n",
" Key | \n",
"
\n",
" \n",
" | Code | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | U+0000 | \n",
" 0 | \n",
" 001 | \n",
" NUL | \n",
"
\n",
" \n",
" | U+0001 | \n",
" 1 | \n",
" 002 | \n",
" Ctrl-A | \n",
"
\n",
" \n",
" | U+0002 | \n",
" 2 | \n",
" 003 | \n",
" Ctrl-B | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Decimal Octal Key\n",
"Code \n",
"U+0000 0 001 NUL\n",
"U+0001 1 002 Ctrl-A\n",
"U+0002 2 003 Ctrl-B"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iloc[:3, :3]"
]
},
{
"cell_type": "markdown",
"id": "48efc21c",
"metadata": {},
"source": [
"There are many ways to select and rearrange the data contained in a pandas object. Below is a brief summary of most of these options for DataFrames:\n",
"\n",
"Type | Note\n",
":--- | :---\n",
"`df[LABEL]` | selects a single column or a sequence of columns from the DataFrame\n",
"`df.loc[LABEL]` | selects a single row or a subset of rows from the DataFrame by label\n",
"`df.loc[:, LABEL]` | selects a single column or a subset of columns from the DataFrame by Label\n",
"`df.loc[LABEL1, LABEL2]` | selects both rows and columns by label\n",
"`df.iloc[INTEGER]` | selects a single row or a subset of rows from the DataFrame by integer position\n",
"`df.iloc[INTEGER1, INTEGER2]` | selects a single column or a subset of columns by integer position\n",
"`df.at[LABEL1, LABEL2]` | selects a single value by row and column label\n",
"`df.iat[INTEGER1, INTEGER2]` | selects a scalar value by row and column position (integers)\n",
"`reindex NEW_INDEX` | selects rows or columns by label\n",
"`get_value`, `set_value` | deprecated since version 0.21.0: use `.at[]` or `.iat[]` instead."
]
}
],
"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
}