{ "cells": [ { "cell_type": "markdown", "id": "5b70dbe9", "metadata": {}, "source": [ "# Arithmetic\n", "\n", "An important function of pandas is the arithmetic behaviour for objects with different indices. When adding objects, if the index pairs are not equal, the corresponding index in the result will be the union of the index pairs. For users with database experience, this is comparable to an automatic [outer join](https://en.wikipedia.org/wiki/Join_(SQL)#Outer_join) on the index labels. Let’s look at an example:" ] }, { "cell_type": "code", "execution_count": 1, "id": "93b775f5", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T12:48:04.248618Z", "iopub.status.busy": "2026-05-17T12:48:04.248310Z", "iopub.status.idle": "2026-05-17T12:48:04.523965Z", "shell.execute_reply": "2026-05-17T12:48:04.523673Z", "shell.execute_reply.started": "2026-05-17T12:48:04.248599Z" } }, "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": "b05a13ac", "metadata": {}, "source": [ "If you add these values, you get:" ] }, { "cell_type": "code", "execution_count": 2, "id": "81c60161", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T12:48:04.524372Z", "iopub.status.busy": "2026-05-17T12:48:04.524259Z", "iopub.status.idle": "2026-05-17T12:48:04.528556Z", "shell.execute_reply": "2026-05-17T12:48:04.528342Z", "shell.execute_reply.started": "2026-05-17T12:48:04.524364Z" } }, "outputs": [ { "data": { "text/plain": [ "0 1.664505\n", "1 -0.366291\n", "2 0.020457\n", "3 1.151139\n", "4 -0.674874\n", "5 NaN\n", "6 NaN\n", "dtype: float64" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 + s2" ] }, { "cell_type": "markdown", "id": "d0b380ee", "metadata": {}, "source": [ "The internal data matching leads to missing values at the points of the labels that do not overlap. Missing values are then passed on in further arithmetic calculations." ] }, { "cell_type": "markdown", "id": "bd4ef5fa", "metadata": {}, "source": [ "For DataFrames, alignment is performed for both rows and columns:" ] }, { "cell_type": "code", "execution_count": 3, "id": "e16d0e94", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T12:48:04.528970Z", "iopub.status.busy": "2026-05-17T12:48:04.528894Z", "iopub.status.idle": "2026-05-17T12:48:04.530994Z", "shell.execute_reply": "2026-05-17T12:48:04.530440Z", "shell.execute_reply.started": "2026-05-17T12:48:04.528962Z" } }, "outputs": [], "source": [ "df1 = pd.DataFrame(rng.normal(size=(5, 3)))\n", "df2 = pd.DataFrame(rng.normal(size=(7, 2)))" ] }, { "cell_type": "markdown", "id": "13ce8463", "metadata": {}, "source": [ "When the two DataFrames are added together, the result is a DataFrame whose index and columns are the unions of those in each of the DataFrames above:" ] }, { "cell_type": "code", "execution_count": 4, "id": "932c7e62", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T12:48:04.531547Z", "iopub.status.busy": "2026-05-17T12:48:04.531439Z", "iopub.status.idle": "2026-05-17T12:48:04.537285Z", "shell.execute_reply": "2026-05-17T12:48:04.537016Z", "shell.execute_reply.started": "2026-05-17T12:48:04.531539Z" } }, "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
0-0.451974-0.041735NaN
1-0.670561-3.538462NaN
2-0.731903-0.933451NaN
30.136283-0.973772NaN
4-0.911367-0.702978NaN
5NaNNaNNaN
6NaNNaNNaN
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 -0.451974 -0.041735 NaN\n", "1 -0.670561 -3.538462 NaN\n", "2 -0.731903 -0.933451 NaN\n", "3 0.136283 -0.973772 NaN\n", "4 -0.911367 -0.702978 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": "5974e184", "metadata": {}, "source": [ "Since column 2 does not appear in both DataFrame objects, its values appear as missing in the result. The same applies to the rows whose labels do not appear in both objects." ] }, { "cell_type": "markdown", "id": "1cd9fe13", "metadata": {}, "source": [ "## Arithmetic methods with fill values\n", "\n", "In arithmetic operations between differently indexed objects, a special value (e.g. `0`) can be useful if an axis label is found in one object but not in the other. The `add` method can pass the `fill_value` argument:" ] }, { "cell_type": "code", "execution_count": 5, "id": "35f89dfd", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T12:48:04.537696Z", "iopub.status.busy": "2026-05-17T12:48:04.537619Z", "iopub.status.idle": "2026-05-17T12:48:04.541394Z", "shell.execute_reply": "2026-05-17T12:48:04.541148Z", "shell.execute_reply.started": "2026-05-17T12:48:04.537689Z" } }, "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
0-0.451974-0.0417350.503665
1-0.670561-3.538462-1.881109
2-0.731903-0.9334510.539550
30.136283-0.973772-1.207889
4-0.911367-0.702978-0.552577
5-1.0191790.678391NaN
6-2.2751962.685174NaN
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 -0.451974 -0.041735 0.503665\n", "1 -0.670561 -3.538462 -1.881109\n", "2 -0.731903 -0.933451 0.539550\n", "3 0.136283 -0.973772 -1.207889\n", "4 -0.911367 -0.702978 -0.552577\n", "5 -1.019179 0.678391 NaN\n", "6 -2.275196 2.685174 NaN" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df12 = df1.add(df2, fill_value=0)\n", "\n", "df12" ] }, { "cell_type": "markdown", "id": "686dc151", "metadata": {}, "source": [ "In the following example, we set the two remaining NaN values to `0`:" ] }, { "cell_type": "code", "execution_count": 6, "id": "7dd4bbe1", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T12:48:04.543107Z", "iopub.status.busy": "2026-05-17T12:48:04.542971Z", "iopub.status.idle": "2026-05-17T12:48:04.544803Z", "shell.execute_reply": "2026-05-17T12:48:04.544555Z", "shell.execute_reply.started": "2026-05-17T12:48:04.543097Z" } }, "outputs": [], "source": [ "df12.iloc[[5, 6], [2]] = 0" ] }, { "cell_type": "code", "execution_count": 7, "id": "a7f447ae", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T12:48:04.545262Z", "iopub.status.busy": "2026-05-17T12:48:04.545177Z", "iopub.status.idle": "2026-05-17T12:48:04.548580Z", "shell.execute_reply": "2026-05-17T12:48:04.548366Z", "shell.execute_reply.started": "2026-05-17T12:48:04.545253Z" } }, "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
0-0.451974-0.0417350.503665
1-0.670561-3.538462-1.881109
2-0.731903-0.9334510.539550
30.136283-0.973772-1.207889
4-0.911367-0.702978-0.552577
5-1.0191790.6783910.000000
6-2.2751962.6851740.000000
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 -0.451974 -0.041735 0.503665\n", "1 -0.670561 -3.538462 -1.881109\n", "2 -0.731903 -0.933451 0.539550\n", "3 0.136283 -0.973772 -1.207889\n", "4 -0.911367 -0.702978 -0.552577\n", "5 -1.019179 0.678391 0.000000\n", "6 -2.275196 2.685174 0.000000" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df12" ] }, { "cell_type": "markdown", "id": "1dbbcce0", "metadata": {}, "source": [ "## Arithmetic methods\n", "\n", "Method | Description\n", ":----- | :----------\n", "`add`, `radd` | methods for addition (`+`)\n", "`sub`, `rsub` | methods for subtraction (`-`)\n", "`div`, `rdiv` | methods for division (`/`)\n", "`floordiv`, `rfloordiv` | methods for floor division (`//`)\n", "`mul`, `rmul` | methods for multiplication (`*`)\n", "`pow`, `rpow` | methods for exponentiation (`**`)\n", "\n", "`r` (English: _reverse_) reverses the method." ] }, { "cell_type": "markdown", "id": "2e634696", "metadata": {}, "source": [ "## Operations between DataFrame and Series\n", "\n", "As with NumPy arrays of different dimensions, the arithmetic between DataFrame and Series is also defined." ] }, { "cell_type": "code", "execution_count": 8, "id": "7920f4b7", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T12:48:04.548991Z", "iopub.status.busy": "2026-05-17T12:48:04.548932Z", "iopub.status.idle": "2026-05-17T12:48:04.552392Z", "shell.execute_reply": "2026-05-17T12:48:04.552138Z", "shell.execute_reply.started": "2026-05-17T12:48:04.548985Z" } }, "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.544941-0.6332911.554287NaNNaN
10.326354-4.130018-0.830487NaNNaN
20.265012-1.5250081.590171NaNNaN
31.133198-1.565328-0.157267NaNNaN
40.085547-1.2945340.498044NaNNaN
5-0.0222640.0868351.050622NaNNaN
6-1.2782812.0936181.050622NaNNaN
\n", "
" ], "text/plain": [ " 0 1 2 3 4\n", "0 0.544941 -0.633291 1.554287 NaN NaN\n", "1 0.326354 -4.130018 -0.830487 NaN NaN\n", "2 0.265012 -1.525008 1.590171 NaN NaN\n", "3 1.133198 -1.565328 -0.157267 NaN NaN\n", "4 0.085547 -1.294534 0.498044 NaN NaN\n", "5 -0.022264 0.086835 1.050622 NaN NaN\n", "6 -1.278281 2.093618 1.050622 NaN NaN" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 + df12" ] }, { "cell_type": "markdown", "id": "36005166", "metadata": {}, "source": [ "If we add `s1` with `df12`, the addition is done once for each line. This is called _broadcasting_. By default, the arithmetic between the DataFrame and the series corresponds to the index of the series in the columns of the DataFrame, with the rows being broadcast down." ] }, { "cell_type": "markdown", "id": "4d57486f", "metadata": {}, "source": [ "If an index value is found neither in the columns of the DataFrame nor in the index of the series, the objects are re-indexed to form the union:" ] }, { "cell_type": "markdown", "id": "0f0f79eb", "metadata": {}, "source": [ "If instead you want to transfer the columns and match the rows, you must use one of the arithmetic methods, for example:" ] }, { "cell_type": "code", "execution_count": 9, "id": "cebe5864", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T12:48:04.552884Z", "iopub.status.busy": "2026-05-17T12:48:04.552814Z", "iopub.status.idle": "2026-05-17T12:48:04.555983Z", "shell.execute_reply": "2026-05-17T12:48:04.555662Z", "shell.execute_reply.started": "2026-05-17T12:48:04.552878Z" } }, "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.2156160.6258551.171255
1-0.445295-3.313196-1.655843
2-1.762067-1.963616-0.490615
30.526549-0.583506-0.817623
4-0.497226-0.288836-0.138436
50.2836801.9812501.302859
6-2.8327902.127580-0.557594
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 0.215616 0.625855 1.171255\n", "1 -0.445295 -3.313196 -1.655843\n", "2 -1.762067 -1.963616 -0.490615\n", "3 0.526549 -0.583506 -0.817623\n", "4 -0.497226 -0.288836 -0.138436\n", "5 0.283680 1.981250 1.302859\n", "6 -2.832790 2.127580 -0.557594" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df12.add(s2, axis=\"index\")" ] }, { "cell_type": "markdown", "id": "21b63ef1", "metadata": {}, "source": [ "The axis number you pass is the axis to be aligned to. In this case, the row index of the DataFrame (`axis='index'` or `axis=0`) is to be adjusted and transmitted." ] }, { "cell_type": "markdown", "id": "9ffae4f8", "metadata": {}, "source": [ "## Function application and mapping\n", "\n", "`numpy.ufunc` (element-wise array methods) also work with pandas objects:" ] }, { "cell_type": "code", "execution_count": 10, "id": "0796f179", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T12:48:04.556373Z", "iopub.status.busy": "2026-05-17T12:48:04.556296Z", "iopub.status.idle": "2026-05-17T12:48:04.559221Z", "shell.execute_reply": "2026-05-17T12:48:04.559021Z", "shell.execute_reply.started": "2026-05-17T12:48:04.556366Z" } }, "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.4519740.0417350.503665
10.6705613.5384621.881109
20.7319030.9334510.539550
30.1362830.9737721.207889
40.9113670.7029780.552577
51.0191790.6783910.000000
62.2751962.6851740.000000
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 0.451974 0.041735 0.503665\n", "1 0.670561 3.538462 1.881109\n", "2 0.731903 0.933451 0.539550\n", "3 0.136283 0.973772 1.207889\n", "4 0.911367 0.702978 0.552577\n", "5 1.019179 0.678391 0.000000\n", "6 2.275196 2.685174 0.000000" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.abs(df12)" ] }, { "cell_type": "markdown", "id": "e00dcebb", "metadata": {}, "source": [ "Another common operation is to apply a function to one-dimensional arrays on each column or row. The [pandas.DataFrame.apply](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html) method does just that:" ] }, { "cell_type": "code", "execution_count": 11, "id": "97d094c1", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T12:48:04.559803Z", "iopub.status.busy": "2026-05-17T12:48:04.559671Z", "iopub.status.idle": "2026-05-17T12:48:04.562656Z", "shell.execute_reply": "2026-05-17T12:48:04.562401Z", "shell.execute_reply.started": "2026-05-17T12:48:04.559793Z" } }, "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
0-0.451974-0.0417350.503665
1-0.670561-3.538462-1.881109
2-0.731903-0.9334510.539550
30.136283-0.973772-1.207889
4-0.911367-0.702978-0.552577
5-1.0191790.6783910.000000
6-2.2751962.6851740.000000
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 -0.451974 -0.041735 0.503665\n", "1 -0.670561 -3.538462 -1.881109\n", "2 -0.731903 -0.933451 0.539550\n", "3 0.136283 -0.973772 -1.207889\n", "4 -0.911367 -0.702978 -0.552577\n", "5 -1.019179 0.678391 0.000000\n", "6 -2.275196 2.685174 0.000000" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df12" ] }, { "cell_type": "code", "execution_count": 12, "id": "0f11be9c", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T12:48:04.563067Z", "iopub.status.busy": "2026-05-17T12:48:04.562998Z", "iopub.status.idle": "2026-05-17T12:48:04.566236Z", "shell.execute_reply": "2026-05-17T12:48:04.566019Z", "shell.execute_reply.started": "2026-05-17T12:48:04.563060Z" } }, "outputs": [ { "data": { "text/plain": [ "0 2.411479\n", "1 6.223636\n", "2 2.420658\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": "6421662b", "metadata": {}, "source": [ "Here the function `minmaxrange()`, which calculates the difference between the maximum and minimum of a row, is called once for each column of the frame. The result is a row with the columns of the frame as index." ] }, { "cell_type": "markdown", "id": "ecd6ad81", "metadata": {}, "source": [ "If you pass `axis=\"columns\"` to `apply`, the function will be called once per line instead:" ] }, { "cell_type": "code", "execution_count": 13, "id": "b54c6f89", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T12:48:04.566777Z", "iopub.status.busy": "2026-05-17T12:48:04.566660Z", "iopub.status.idle": "2026-05-17T12:48:04.569426Z", "shell.execute_reply": "2026-05-17T12:48:04.569169Z", "shell.execute_reply.started": "2026-05-17T12:48:04.566767Z" } }, "outputs": [ { "data": { "text/plain": [ "0 0.955639\n", "1 2.867901\n", "2 1.473001\n", "3 1.344172\n", "4 0.358790\n", "5 1.697570\n", "6 4.960370\n", "dtype: float64" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df12.apply(minmaxrange, axis=\"columns\")" ] }, { "cell_type": "markdown", "id": "677910f9", "metadata": {}, "source": [ "Many of the most common array statistics (such as `sum` and `mean`) are DataFrame methods, so the use of `apply` is not necessary." ] }, { "cell_type": "markdown", "id": "ff49ea56", "metadata": {}, "source": [ "The function passed to apply does not have to return a single value; it can also return a series with multiple values:" ] }, { "cell_type": "code", "execution_count": 14, "id": "4abc03d5", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T12:48:04.569928Z", "iopub.status.busy": "2026-05-17T12:48:04.569822Z", "iopub.status.idle": "2026-05-17T12:48:04.573922Z", "shell.execute_reply": "2026-05-17T12:48:04.573612Z", "shell.execute_reply.started": "2026-05-17T12:48:04.569920Z" } }, "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-2.275196-3.538462-1.881109
max0.1362832.6851740.539550
\n", "
" ], "text/plain": [ " 0 1 2\n", "min -2.275196 -3.538462 -1.881109\n", "max 0.136283 2.685174 0.539550" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def minmax(x):\n", " return pd.Series([x.min(), x.max()], index=[\"min\", \"max\"])\n", "\n", "\n", "df12.apply(minmax)" ] }, { "cell_type": "markdown", "id": "06dc9053", "metadata": {}, "source": [ "You can also use element-wise Python functions. Suppose you want to round each floating point value in `df12` to two decimal places, you can do this with [pandas.DataFrame.map](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.map.html):" ] }, { "cell_type": "code", "execution_count": 15, "id": "3b470cc7", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T12:48:04.574443Z", "iopub.status.busy": "2026-05-17T12:48:04.574362Z", "iopub.status.idle": "2026-05-17T12:48:04.577790Z", "shell.execute_reply": "2026-05-17T12:48:04.577554Z", "shell.execute_reply.started": "2026-05-17T12:48:04.574436Z" } }, "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
0-0.45-0.040.50
1-0.67-3.54-1.88
2-0.73-0.930.54
30.14-0.97-1.21
4-0.91-0.70-0.55
5-1.020.680.00
6-2.282.690.00
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 -0.45 -0.04 0.50\n", "1 -0.67 -3.54 -1.88\n", "2 -0.73 -0.93 0.54\n", "3 0.14 -0.97 -1.21\n", "4 -0.91 -0.70 -0.55\n", "5 -1.02 0.68 0.00\n", "6 -2.28 2.69 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": "787b95cd", "metadata": {}, "source": [ "The `map` method can also be applied to Series:" ] }, { "cell_type": "code", "execution_count": 16, "id": "3aaaeef4", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T12:48:04.578200Z", "iopub.status.busy": "2026-05-17T12:48:04.578129Z", "iopub.status.idle": "2026-05-17T12:48:04.580557Z", "shell.execute_reply": "2026-05-17T12:48:04.580334Z", "shell.execute_reply.started": "2026-05-17T12:48:04.578193Z" } }, "outputs": [ { "data": { "text/plain": [ "0 0.50\n", "1 -1.88\n", "2 0.54\n", "3 -1.21\n", "4 -0.55\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 }