{
"cells": [
{
"cell_type": "markdown",
"id": "765a0354",
"metadata": {},
"source": [
"# Adding, changing and deleting data\n",
"\n",
"With many data sets, you may want to perform a transformation based on the values in an array, series or column in a DataFrame. To do this, we look at the first Unicode characters:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "75059c0e",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-21T13:06:13.506747Z",
"iopub.status.busy": "2026-05-21T13:06:13.506251Z",
"iopub.status.idle": "2026-05-21T13:06:13.779093Z",
"shell.execute_reply": "2026-05-21T13:06:13.778792Z",
"shell.execute_reply.started": "2026-05-21T13:06:13.506726Z"
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0aa26fb0",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-21T13:06:13.958166Z",
"iopub.status.busy": "2026-05-21T13:06:13.957955Z",
"iopub.status.idle": "2026-05-21T13:06:13.964772Z",
"shell.execute_reply": "2026-05-21T13:06:13.964541Z",
"shell.execute_reply.started": "2026-05-21T13:06:13.958153Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Code | \n",
" Decimal | \n",
" Octal | \n",
" Key | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" U+0000 | \n",
" 0 | \n",
" 001 | \n",
" NUL | \n",
"
\n",
" \n",
" | 1 | \n",
" U+0001 | \n",
" 1 | \n",
" 002 | \n",
" Ctrl-A | \n",
"
\n",
" \n",
" | 2 | \n",
" U+0002 | \n",
" 2 | \n",
" 003 | \n",
" Ctrl-B | \n",
"
\n",
" \n",
" | 3 | \n",
" U+0003 | \n",
" 3 | \n",
" 004 | \n",
" Ctrl-C | \n",
"
\n",
" \n",
" | 4 | \n",
" U+0004 | \n",
" 4 | \n",
" 004 | \n",
" Ctrl-D | \n",
"
\n",
" \n",
" | 5 | \n",
" U+0005 | \n",
" 5 | \n",
" 005 | \n",
" Ctrl-E | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Code Decimal Octal Key\n",
"0 U+0000 0 001 NUL\n",
"1 U+0001 1 002 Ctrl-A\n",
"2 U+0002 2 003 Ctrl-B\n",
"3 U+0003 3 004 Ctrl-C\n",
"4 U+0004 4 004 Ctrl-D\n",
"5 U+0005 5 005 Ctrl-E"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame(\n",
" {\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",
"\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "15da5652",
"metadata": {},
"source": [
"## Adding data\n",
"\n",
"Suppose you want to add a column in which characters are assigned to the `C0` or `C1` control code:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b96f46e7",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-21T13:06:15.130061Z",
"iopub.status.busy": "2026-05-21T13:06:15.129620Z",
"iopub.status.idle": "2026-05-21T13:06:15.135810Z",
"shell.execute_reply": "2026-05-21T13:06:15.134809Z",
"shell.execute_reply.started": "2026-05-21T13:06:15.130030Z"
}
},
"outputs": [],
"source": [
"control_code = {\n",
" \"u+0000\": \"C0\",\n",
" \"u+0001\": \"C0\",\n",
" \"u+0002\": \"C0\",\n",
" \"u+0003\": \"C0\",\n",
" \"u+0004\": \"C0\",\n",
" \"u+0005\": \"C0\",\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "14593018",
"metadata": {},
"source": [
"The `map` method for a series accepts a function or a dict-like object containing a mapping, but here we have a small problem because some of the codes in `control_code` are lowercase, but not in our DataFrame. Therefore, we need to convert each value to lowercase using the `str.lower` method:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "036a2cb7",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-21T13:06:16.545778Z",
"iopub.status.busy": "2026-05-21T13:06:16.545345Z",
"iopub.status.idle": "2026-05-21T13:06:16.553705Z",
"shell.execute_reply": "2026-05-21T13:06:16.553251Z",
"shell.execute_reply.started": "2026-05-21T13:06:16.545747Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0 u+0000\n",
"1 u+0001\n",
"2 u+0002\n",
"3 u+0003\n",
"4 u+0004\n",
"5 u+0005\n",
"Name: Code, dtype: object"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lowercased = df[\"Code\"].str.lower()\n",
"\n",
"lowercased"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "a21615a2",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-21T13:06:16.865336Z",
"iopub.status.busy": "2026-05-21T13:06:16.864827Z",
"iopub.status.idle": "2026-05-21T13:06:16.875727Z",
"shell.execute_reply": "2026-05-21T13:06:16.875017Z",
"shell.execute_reply.started": "2026-05-21T13:06:16.865288Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Code | \n",
" Decimal | \n",
" Octal | \n",
" Key | \n",
" Control code | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" U+0000 | \n",
" 0 | \n",
" 001 | \n",
" NUL | \n",
" C0 | \n",
"
\n",
" \n",
" | 1 | \n",
" U+0001 | \n",
" 1 | \n",
" 002 | \n",
" Ctrl-A | \n",
" C0 | \n",
"
\n",
" \n",
" | 2 | \n",
" U+0002 | \n",
" 2 | \n",
" 003 | \n",
" Ctrl-B | \n",
" C0 | \n",
"
\n",
" \n",
" | 3 | \n",
" U+0003 | \n",
" 3 | \n",
" 004 | \n",
" Ctrl-C | \n",
" C0 | \n",
"
\n",
" \n",
" | 4 | \n",
" U+0004 | \n",
" 4 | \n",
" 004 | \n",
" Ctrl-D | \n",
" C0 | \n",
"
\n",
" \n",
" | 5 | \n",
" U+0005 | \n",
" 5 | \n",
" 005 | \n",
" Ctrl-E | \n",
" C0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Code Decimal Octal Key Control code\n",
"0 U+0000 0 001 NUL C0\n",
"1 U+0001 1 002 Ctrl-A C0\n",
"2 U+0002 2 003 Ctrl-B C0\n",
"3 U+0003 3 004 Ctrl-C C0\n",
"4 U+0004 4 004 Ctrl-D C0\n",
"5 U+0005 5 005 Ctrl-E C0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[\"Control code\"] = lowercased.map(control_code)\n",
"\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "7a1a01ca",
"metadata": {},
"source": [
"We could also have passed a function that does all the work:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "6cedb9de",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-21T13:06:18.847325Z",
"iopub.status.busy": "2026-05-21T13:06:18.846732Z",
"iopub.status.idle": "2026-05-21T13:06:18.856513Z",
"shell.execute_reply": "2026-05-21T13:06:18.855992Z",
"shell.execute_reply.started": "2026-05-21T13:06:18.847264Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0 C0\n",
"1 C0\n",
"2 C0\n",
"3 C0\n",
"4 C0\n",
"5 C0\n",
"Name: Code, dtype: object"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[\"Code\"].map(lambda x: control_code[x.lower()])"
]
},
{
"cell_type": "markdown",
"id": "e7f5d504",
"metadata": {},
"source": [
"Using `map` is a convenient way to perform element-by-element transformations and other data cleansing operations."
]
},
{
"cell_type": "markdown",
"id": "eb9bfab9",
"metadata": {},
"source": [
"## Modifying data\n",
"\n",
"The [replace](https://pandas.pydata.org/docs/reference/api/pandas.Series.replace.html) method can be used to replace certain values with others."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "904b390c-3fb0-494a-a4a5-8197054cce9a",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-21T13:06:20.146051Z",
"iopub.status.busy": "2026-05-21T13:06:20.145621Z",
"iopub.status.idle": "2026-05-21T13:06:20.150858Z",
"shell.execute_reply": "2026-05-21T13:06:20.149898Z",
"shell.execute_reply.started": "2026-05-21T13:06:20.146020Z"
}
},
"outputs": [],
"source": [
"s = pd.Series([\"Manpower\", \"man-made\", np.nan])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "5643fd4b-3eba-4972-8562-aaf22e200fb4",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-21T13:06:20.652508Z",
"iopub.status.busy": "2026-05-21T13:06:20.651936Z",
"iopub.status.idle": "2026-05-21T13:06:20.658290Z",
"shell.execute_reply": "2026-05-21T13:06:20.657869Z",
"shell.execute_reply.started": "2026-05-21T13:06:20.652466Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0 Manpower\n",
"1 man-made\n",
"2 NaN\n",
"dtype: object"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.replace(\"Man\", \"Personal\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "b8c17bb0-5775-44e7-916b-734c08c77827",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-21T13:06:20.939310Z",
"iopub.status.busy": "2026-05-21T13:06:20.939015Z",
"iopub.status.idle": "2026-05-21T13:06:20.943989Z",
"shell.execute_reply": "2026-05-21T13:06:20.943490Z",
"shell.execute_reply.started": "2026-05-21T13:06:20.939291Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0 Personalpower\n",
"1 Personal-made\n",
"2 NaN\n",
"dtype: object"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.replace(\"[Mm]an\", \"Personal\", regex=True)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "a4c66a61-a9ba-4742-80a2-ea2df4ce19f6",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-21T13:06:22.199722Z",
"iopub.status.busy": "2026-05-21T13:06:22.198918Z",
"iopub.status.idle": "2026-05-21T13:06:22.206929Z",
"shell.execute_reply": "2026-05-21T13:06:22.206432Z",
"shell.execute_reply.started": "2026-05-21T13:06:22.199679Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0 Personalpower\n",
"1 Personal-made\n",
"2 0\n",
"dtype: object"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.replace([\"[Mm]an\", np.nan], [\"Personal\", 0], regex=True)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "5a389415-6bce-4d3b-81dc-cf0e8c62a28c",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-21T13:06:22.628523Z",
"iopub.status.busy": "2026-05-21T13:06:22.627941Z",
"iopub.status.idle": "2026-05-21T13:06:22.635923Z",
"shell.execute_reply": "2026-05-21T13:06:22.635515Z",
"shell.execute_reply.started": "2026-05-21T13:06:22.628479Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0 Personalpower\n",
"1 Personal-made\n",
"2 3\n",
"dtype: object"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.replace([\"[Mm]an\", np.nan], [\"Personal\", len(s)], regex=True)"
]
},
{
"cell_type": "markdown",
"id": "40c0aeee-95af-4138-af68-a06185144f6f",
"metadata": {},
"source": [
"\n",
"\n",
"**See also:**\n",
"\n",
"* [Managing missing data with pandas](../../clean-prep/nulls.ipynb)\n",
"
"
]
},
{
"cell_type": "markdown",
"id": "0f2b0a9f",
"metadata": {},
"source": [
"## Deleting data"
]
},
{
"cell_type": "markdown",
"id": "5b3e04fd-2af9-43b0-b857-172981c26d8c",
"metadata": {},
"source": [
"Deleting one or more entries from an axis is easy if you already have an index array or list without those entries."
]
},
{
"cell_type": "markdown",
"id": "16cea6c7",
"metadata": {},
"source": [
"Since this may require a little set theory, we return the drop method as a new object without the deleted value(s):"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "07f0e4e0",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-21T13:06:48.517675Z",
"iopub.status.busy": "2026-05-21T13:06:48.517221Z",
"iopub.status.idle": "2026-05-21T13:06:48.523159Z",
"shell.execute_reply": "2026-05-21T13:06:48.522783Z",
"shell.execute_reply.started": "2026-05-21T13:06:48.517656Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0 0.957904\n",
"1 0.239710\n",
"2 0.666261\n",
"3 0.303127\n",
"4 0.000573\n",
"5 0.508528\n",
"6 0.329187\n",
"dtype: float64"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rng = np.random.default_rng()\n",
"\n",
"s = pd.Series(rng.random(7))\n",
"s"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "c33dd6b2",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-21T13:08:23.065906Z",
"iopub.status.busy": "2026-05-21T13:08:23.065349Z",
"iopub.status.idle": "2026-05-21T13:08:23.074865Z",
"shell.execute_reply": "2026-05-21T13:08:23.073913Z",
"shell.execute_reply.started": "2026-05-21T13:08:23.065860Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0 0.957904\n",
"1 0.239710\n",
"3 0.303127\n",
"4 0.000573\n",
"5 0.508528\n",
"6 0.329187\n",
"dtype: float64"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.drop(2)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "9a41f1fa",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-21T13:08:31.261111Z",
"iopub.status.busy": "2026-05-21T13:08:31.260821Z",
"iopub.status.idle": "2026-05-21T13:08:31.266019Z",
"shell.execute_reply": "2026-05-21T13:08:31.265515Z",
"shell.execute_reply.started": "2026-05-21T13:08:31.261093Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0 0.957904\n",
"1 0.239710\n",
"4 0.000573\n",
"5 0.508528\n",
"6 0.329187\n",
"dtype: float64"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.drop([2, 3])"
]
},
{
"cell_type": "markdown",
"id": "362891d3",
"metadata": {},
"source": [
"With DataFrames, index values can be deleted on both axes. To illustrate this, we will first create a sample DataFrame:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "88180f1d",
"metadata": {
"execution": {
"iopub.execute_input": "2025-11-18T20:51:03.725360Z",
"iopub.status.busy": "2025-11-18T20:51:03.725057Z",
"iopub.status.idle": "2025-11-18T20:51:03.736238Z",
"shell.execute_reply": "2025-11-18T20:51:03.735813Z",
"shell.execute_reply.started": "2025-11-18T20:51:03.725342Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Code | \n",
" Decimal | \n",
" Octal | \n",
" Key | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" U+0000 | \n",
" 0 | \n",
" 001 | \n",
" NUL | \n",
"
\n",
" \n",
" | 1 | \n",
" U+0001 | \n",
" 1 | \n",
" 002 | \n",
" Ctrl-A | \n",
"
\n",
" \n",
" | 2 | \n",
" U+0002 | \n",
" 2 | \n",
" 003 | \n",
" Ctrl-B | \n",
"
\n",
" \n",
" | 3 | \n",
" U+0003 | \n",
" 3 | \n",
" 004 | \n",
" Ctrl-C | \n",
"
\n",
" \n",
" | 4 | \n",
" U+0004 | \n",
" 4 | \n",
" 004 | \n",
" Ctrl-D | \n",
"
\n",
" \n",
" | 5 | \n",
" U+0005 | \n",
" 5 | \n",
" 005 | \n",
" Ctrl-E | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Code Decimal Octal Key\n",
"0 U+0000 0 001 NUL\n",
"1 U+0001 1 002 Ctrl-A\n",
"2 U+0002 2 003 Ctrl-B\n",
"3 U+0003 3 004 Ctrl-C\n",
"4 U+0004 4 004 Ctrl-D\n",
"5 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",
"\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "49667d00",
"metadata": {
"execution": {
"iopub.execute_input": "2025-11-18T20:51:05.772465Z",
"iopub.status.busy": "2025-11-18T20:51:05.772170Z",
"iopub.status.idle": "2025-11-18T20:51:05.779465Z",
"shell.execute_reply": "2025-11-18T20:51:05.779038Z",
"shell.execute_reply.started": "2025-11-18T20:51:05.772445Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Code | \n",
" Decimal | \n",
" Octal | \n",
" Key | \n",
"
\n",
" \n",
" \n",
" \n",
" | 2 | \n",
" U+0002 | \n",
" 2 | \n",
" 003 | \n",
" Ctrl-B | \n",
"
\n",
" \n",
" | 3 | \n",
" U+0003 | \n",
" 3 | \n",
" 004 | \n",
" Ctrl-C | \n",
"
\n",
" \n",
" | 4 | \n",
" U+0004 | \n",
" 4 | \n",
" 004 | \n",
" Ctrl-D | \n",
"
\n",
" \n",
" | 5 | \n",
" U+0005 | \n",
" 5 | \n",
" 005 | \n",
" Ctrl-E | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Code Decimal Octal Key\n",
"2 U+0002 2 003 Ctrl-B\n",
"3 U+0003 3 004 Ctrl-C\n",
"4 U+0004 4 004 Ctrl-D\n",
"5 U+0005 5 005 Ctrl-E"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.drop([0, 1])"
]
},
{
"cell_type": "markdown",
"id": "1f5543d0",
"metadata": {},
"source": [
"You can also remove values from the columns by passing `axis=1` or `axis=\"columns\"`:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "09a66ce8",
"metadata": {
"execution": {
"iopub.execute_input": "2025-11-18T20:51:09.208563Z",
"iopub.status.busy": "2025-11-18T20:51:09.208050Z",
"iopub.status.idle": "2025-11-18T20:51:09.218064Z",
"shell.execute_reply": "2025-11-18T20:51:09.217570Z",
"shell.execute_reply.started": "2025-11-18T20:51:09.208528Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Code | \n",
" Octal | \n",
" Key | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" U+0000 | \n",
" 001 | \n",
" NUL | \n",
"
\n",
" \n",
" | 1 | \n",
" U+0001 | \n",
" 002 | \n",
" Ctrl-A | \n",
"
\n",
" \n",
" | 2 | \n",
" U+0002 | \n",
" 003 | \n",
" Ctrl-B | \n",
"
\n",
" \n",
" | 3 | \n",
" U+0003 | \n",
" 004 | \n",
" Ctrl-C | \n",
"
\n",
" \n",
" | 4 | \n",
" U+0004 | \n",
" 004 | \n",
" Ctrl-D | \n",
"
\n",
" \n",
" | 5 | \n",
" U+0005 | \n",
" 005 | \n",
" Ctrl-E | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Code Octal Key\n",
"0 U+0000 001 NUL\n",
"1 U+0001 002 Ctrl-A\n",
"2 U+0002 003 Ctrl-B\n",
"3 U+0003 004 Ctrl-C\n",
"4 U+0004 004 Ctrl-D\n",
"5 U+0005 005 Ctrl-E"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.drop(\"Decimal\", axis=1)"
]
},
{
"cell_type": "markdown",
"id": "54dbbf32",
"metadata": {},
"source": [
"Many functions, such as `drop`, which change the size or shape of an array or DataFrame, can manipulate an object in place without returning a new object:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "24503f8c",
"metadata": {
"execution": {
"iopub.execute_input": "2025-11-18T20:51:12.164912Z",
"iopub.status.busy": "2025-11-18T20:51:12.164456Z",
"iopub.status.idle": "2025-11-18T20:51:12.171419Z",
"shell.execute_reply": "2025-11-18T20:51:12.170786Z",
"shell.execute_reply.started": "2025-11-18T20:51:12.164890Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Code | \n",
" Decimal | \n",
" Octal | \n",
" Key | \n",
"
\n",
" \n",
" \n",
" \n",
" | 1 | \n",
" U+0001 | \n",
" 1 | \n",
" 002 | \n",
" Ctrl-A | \n",
"
\n",
" \n",
" | 2 | \n",
" U+0002 | \n",
" 2 | \n",
" 003 | \n",
" Ctrl-B | \n",
"
\n",
" \n",
" | 3 | \n",
" U+0003 | \n",
" 3 | \n",
" 004 | \n",
" Ctrl-C | \n",
"
\n",
" \n",
" | 4 | \n",
" U+0004 | \n",
" 4 | \n",
" 004 | \n",
" Ctrl-D | \n",
"
\n",
" \n",
" | 5 | \n",
" U+0005 | \n",
" 5 | \n",
" 005 | \n",
" Ctrl-E | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Code Decimal Octal Key\n",
"1 U+0001 1 002 Ctrl-A\n",
"2 U+0002 2 003 Ctrl-B\n",
"3 U+0003 3 004 Ctrl-C\n",
"4 U+0004 4 004 Ctrl-D\n",
"5 U+0005 5 005 Ctrl-E"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.drop(0, inplace=True)\n",
"\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "69ede8c6",
"metadata": {},
"source": [
"\n",
"\n",
"**Warning:**\n",
"\n",
"Be careful with the `inplace` function, as the data will be irretrievably deleted."
]
},
{
"cell_type": "markdown",
"id": "1dd0eca6-79d3-48ef-aad0-0413e4d8d203",
"metadata": {},
"source": [
"
\n",
"\n",
"**See also:**\n",
"\n",
"* [Deduplicate data](../../clean-prep/deduplicate.ipynb)\n",
"
"
]
}
],
"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
}