{ "cells": [ { "cell_type": "markdown", "id": "2a19202c", "metadata": {}, "source": [ "# Aggregation\n", "\n", "Aggregations refer to any data transformation that produces scalar values from arrays. In the previous examples, several of them were used, including `count` and `sum`. You may now be wondering what happens when you apply `sum()` to a `GroupBy` object. Optimised implementations exist for many common aggregations, such as the one in the following table. However, they are not limited to this set of methods.\n", "\n", "Function name Description\n", "\n", "Function name | Description\n", ":------------ | :----------\n", "`any`, `all` | Returns `True` if one (or more) or all of the non-NA values are truthy\n", "`count` | Number of non-NA values\n", "`cummin`, `cummax` | Cumulative minimum and maximum of the non-NA values\n", "`cumsum` | Cumulative sum of the non-NA values\n", "`cumprod` | Cumulative product of non-NA values\n", "`first`, `last` | First and last non-NA values\n", "`mean` | Mean of the non-NA values\n", "`median` | Arithmetic median of the non-NA values\n", "`min`, `max` | Minimum and maximum of the non-NA values\n", "`nth` | Retrieval of the nth largest value\n", "`ohlc` | calculates the four *open-high-low-close* statistics for time series-like data\n", "`prod` | Product of the non-NA values\n", "`quantile` | calculates the sample quantile\n", "`rank` | Ordinal ranks of non-NA values, as when calling `Series.rank`\n", "`sum` | Sum of non-NA values\n", "`std`, `var` | Standard deviation and variance of the sample\n", "\n", "You can use your own aggregations and also call any method that is also defined for the grouped object. For example, the `Series` method `nsmallest` selects the smallest requested number of values from the data.\n", "\n", "Although `nsmallest` is not explicitly implemented for `GroupBy`, we can still use it with a non-optimised implementation. Internally, `GroupBy` decomposes the `Series`, calls `df.nsmallest(n)` for each part and then merges these results in the result object:" ] }, { "cell_type": "code", "execution_count": 1, "id": "f83ce6f3", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "id": "3e437b8d", "metadata": {}, "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", "
Title2021-122022-012022-02
0Jupyter Tutorial30134.033295.019651.0
1Jupyter Tutorial6073.07716.06547.0
2PyViz Tutorial4873.03930.02573.0
3NoneNaNNaNNaN
4Python Basics427.0276.0525.0
5Python Basics95.0226.0157.0
\n", "
" ], "text/plain": [ " Title 2021-12 2022-01 2022-02\n", "0 Jupyter Tutorial 30134.0 33295.0 19651.0\n", "1 Jupyter Tutorial 6073.0 7716.0 6547.0\n", "2 PyViz Tutorial 4873.0 3930.0 2573.0\n", "3 None NaN NaN NaN\n", "4 Python Basics 427.0 276.0 525.0\n", "5 Python Basics 95.0 226.0 157.0" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame(\n", " {\n", " \"Title\": [\n", " \"Jupyter Tutorial\",\n", " \"Jupyter Tutorial\",\n", " \"PyViz Tutorial\",\n", " None,\n", " \"Python Basics\",\n", " \"Python Basics\",\n", " ],\n", " \"2021-12\": [30134, 6073, 4873, None, 427, 95],\n", " \"2022-01\": [33295, 7716, 3930, None, 276, 226],\n", " \"2022-02\": [19651, 6547, 2573, None, 525, 157],\n", " }\n", ")\n", "\n", "df" ] }, { "cell_type": "code", "execution_count": 3, "id": "450dc25f", "metadata": {}, "outputs": [], "source": [ "grouped = df.groupby(\"Title\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "c41de39e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Title \n", "Jupyter Tutorial 1 7716.0\n", "PyViz Tutorial 2 3930.0\n", "Python Basics 5 226.0\n", "Name: 2022-01, dtype: float64" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grouped[\"2022-01\"].nsmallest(1)" ] }, { "cell_type": "markdown", "id": "70e50b36", "metadata": {}, "source": [ "To use a custom aggregation function, pass any function that aggregates an array to the `aggregate` or `agg` method:" ] }, { "cell_type": "code", "execution_count": 5, "id": "2195bdb0", "metadata": {}, "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", "
2021-122022-012022-02
Title
Jupyter Tutorial24061.025579.013104.0
PyViz Tutorial0.00.00.0
Python Basics332.050.0368.0
\n", "
" ], "text/plain": [ " 2021-12 2022-01 2022-02\n", "Title \n", "Jupyter Tutorial 24061.0 25579.0 13104.0\n", "PyViz Tutorial 0.0 0.0 0.0\n", "Python Basics 332.0 50.0 368.0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def range(arr):\n", " return arr.max() - arr.min()\n", "\n", "grouped.agg(range)" ] }, { "cell_type": "markdown", "id": "b9107b14", "metadata": {}, "source": [ "You will find that some methods like `describe` also work, even though they are not strictly speaking aggregations:" ] }, { "cell_type": "code", "execution_count": 6, "id": "5230dcb5", "metadata": {}, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
2021-122022-012022-02
countmeanstdmin25%50%75%maxcountmean...75%maxcountmeanstdmin25%50%75%max
Title
Jupyter Tutorial2.018103.517013.6962626073.012088.2518103.524118.7530134.02.020505.5...26900.2533295.02.013099.09265.9272616547.09823.013099.016375.019651.0
PyViz Tutorial1.04873.0NaN4873.04873.004873.04873.004873.01.03930.0...3930.003930.01.02573.0NaN2573.02573.02573.02573.02573.0
Python Basics2.0261.0234.75945195.0178.00261.0344.00427.02.0251.0...263.50276.02.0341.0260.215295157.0249.0341.0433.0525.0
\n", "

3 rows × 24 columns

\n", "
" ], "text/plain": [ " 2021-12 \\\n", " count mean std min 25% 50% \n", "Title \n", "Jupyter Tutorial 2.0 18103.5 17013.696262 6073.0 12088.25 18103.5 \n", "PyViz Tutorial 1.0 4873.0 NaN 4873.0 4873.00 4873.0 \n", "Python Basics 2.0 261.0 234.759451 95.0 178.00 261.0 \n", "\n", " 2022-01 ... \\\n", " 75% max count mean ... 75% max \n", "Title ... \n", "Jupyter Tutorial 24118.75 30134.0 2.0 20505.5 ... 26900.25 33295.0 \n", "PyViz Tutorial 4873.00 4873.0 1.0 3930.0 ... 3930.00 3930.0 \n", "Python Basics 344.00 427.0 2.0 251.0 ... 263.50 276.0 \n", "\n", " 2022-02 \\\n", " count mean std min 25% 50% \n", "Title \n", "Jupyter Tutorial 2.0 13099.0 9265.927261 6547.0 9823.0 13099.0 \n", "PyViz Tutorial 1.0 2573.0 NaN 2573.0 2573.0 2573.0 \n", "Python Basics 2.0 341.0 260.215295 157.0 249.0 341.0 \n", "\n", " \n", " 75% max \n", "Title \n", "Jupyter Tutorial 16375.0 19651.0 \n", "PyViz Tutorial 2573.0 2573.0 \n", "Python Basics 433.0 525.0 \n", "\n", "[3 rows x 24 columns]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grouped.describe()" ] }, { "cell_type": "markdown", "id": "c6130a2c", "metadata": {}, "source": [ "
\n", "\n", "**Note:**\n", "\n", "Custom aggregation functions are generally much slower than the optimised functions in the table above. This is because there is some extra work involved in creating the intermediate data sets for the group (function calls, reordering of data).\n", "
" ] }, { "cell_type": "markdown", "id": "4bb1a299", "metadata": {}, "source": [ "## Additional functions column by column\n", "\n", "As we have already seen, aggregating a `Series` or all columns of a `DataFrame` is a matter of using `aggregate` (or `agg`) with the desired function or calling a method such as `mean` or `std`. However, it is more common to aggregate simultaneously with another function depending on the column or with multiple functions." ] }, { "cell_type": "code", "execution_count": 7, "id": "04014bdf", "metadata": {}, "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", "
2021-122022-012022-02
Title
Jupyter Tutorial18103.520505.513099.0
PyViz Tutorial4873.03930.02573.0
Python Basics261.0251.0341.0
\n", "
" ], "text/plain": [ " 2021-12 2022-01 2022-02\n", "Title \n", "Jupyter Tutorial 18103.5 20505.5 13099.0\n", "PyViz Tutorial 4873.0 3930.0 2573.0\n", "Python Basics 261.0 251.0 341.0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grouped.agg(\"mean\")" ] }, { "cell_type": "markdown", "id": "567067e4", "metadata": {}, "source": [ "If you pass a list of functions or function names instead, you will get back a `DataFrame` with column names from the functions:" ] }, { "cell_type": "code", "execution_count": 8, "id": "bf0c0e40", "metadata": {}, "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", " \n", " \n", "
2021-122022-012022-02
meanstdrangemeanstdrangemeanstdrange
Title
Jupyter Tutorial18103.517013.69626224061.020505.518087.08435625579.013099.09265.92726113104.0
PyViz Tutorial4873.0NaN0.03930.0NaN0.02573.0NaN0.0
Python Basics261.0234.759451332.0251.035.35533950.0341.0260.215295368.0
\n", "
" ], "text/plain": [ " 2021-12 2022-01 \\\n", " mean std range mean std \n", "Title \n", "Jupyter Tutorial 18103.5 17013.696262 24061.0 20505.5 18087.084356 \n", "PyViz Tutorial 4873.0 NaN 0.0 3930.0 NaN \n", "Python Basics 261.0 234.759451 332.0 251.0 35.355339 \n", "\n", " 2022-02 \n", " range mean std range \n", "Title \n", "Jupyter Tutorial 25579.0 13099.0 9265.927261 13104.0 \n", "PyViz Tutorial 0.0 2573.0 NaN 0.0 \n", "Python Basics 50.0 341.0 260.215295 368.0 " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grouped.agg([\"mean\", \"std\", range])" ] }, { "cell_type": "markdown", "id": "3f2ee9f9", "metadata": {}, "source": [ "Here we have passed `agg` a list of aggregation functions to be evaluated independently for the data groups." ] }, { "cell_type": "markdown", "id": "b22d6e1f", "metadata": {}, "source": [ "You don’t need to accept the names that `GroupBy` gives to the columns; in particular, lambda functions have the name ``, which makes them difficult to identify. When you pass a list of tuples, the first element of each tuple is used as the column name in the DataFrame:" ] }, { "cell_type": "code", "execution_count": 9, "id": "1c5b71f9", "metadata": {}, "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", " \n", " \n", "
2021-122022-012022-02
MeanStandard deviationRangeMeanStandard deviationRangeMeanStandard deviationRange
Title
Jupyter Tutorial18103.517013.69626224061.020505.518087.08435625579.013099.09265.92726113104.0
PyViz Tutorial4873.0NaN0.03930.0NaN0.02573.0NaN0.0
Python Basics261.0234.759451332.0251.035.35533950.0341.0260.215295368.0
\n", "
" ], "text/plain": [ " 2021-12 2022-01 \\\n", " Mean Standard deviation Range Mean \n", "Title \n", "Jupyter Tutorial 18103.5 17013.696262 24061.0 20505.5 \n", "PyViz Tutorial 4873.0 NaN 0.0 3930.0 \n", "Python Basics 261.0 234.759451 332.0 251.0 \n", "\n", " 2022-02 \\\n", " Standard deviation Range Mean Standard deviation \n", "Title \n", "Jupyter Tutorial 18087.084356 25579.0 13099.0 9265.927261 \n", "PyViz Tutorial NaN 0.0 2573.0 NaN \n", "Python Basics 35.355339 50.0 341.0 260.215295 \n", "\n", " \n", " Range \n", "Title \n", "Jupyter Tutorial 13104.0 \n", "PyViz Tutorial 0.0 \n", "Python Basics 368.0 " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grouped.agg(\n", " [(\"Mean\", \"mean\"), (\"Standard deviation\", \"std\"), (\"Range\", range)]\n", ")" ] }, { "cell_type": "markdown", "id": "938b2550", "metadata": {}, "source": [ "With a `DataFrame`, you have the option of specifying a list of functions to be applied to all columns or to different functions per column. Let’s say we want to calculate the same three statistics for the columns:" ] }, { "cell_type": "code", "execution_count": 10, "id": "71712e52", "metadata": {}, "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", " \n", " \n", "
2021-122022-012022-02
countmeanmaxcountmeanmaxcountmeanmax
Title
Jupyter Tutorial218103.530134.0220505.533295.0213099.019651.0
PyViz Tutorial14873.04873.013930.03930.012573.02573.0
Python Basics2261.0427.02251.0276.02341.0525.0
\n", "
" ], "text/plain": [ " 2021-12 2022-01 2022-02 \\\n", " count mean max count mean max count \n", "Title \n", "Jupyter Tutorial 2 18103.5 30134.0 2 20505.5 33295.0 2 \n", "PyViz Tutorial 1 4873.0 4873.0 1 3930.0 3930.0 1 \n", "Python Basics 2 261.0 427.0 2 251.0 276.0 2 \n", "\n", " \n", " mean max \n", "Title \n", "Jupyter Tutorial 13099.0 19651.0 \n", "PyViz Tutorial 2573.0 2573.0 \n", "Python Basics 341.0 525.0 " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stats = [\"count\", \"mean\", \"max\"]\n", "\n", "evaluations = grouped.agg(stats)\n", "\n", "evaluations" ] }, { "cell_type": "markdown", "id": "bb675e38", "metadata": {}, "source": [ "As you can see, the resulting `DataFrame` has hierarchical columns, just as you would get if you aggregated each column separately and used [pandas.concat](https://pandas.pydata.org/docs/reference/api/pandas.concat.html) to join the results together, using the column names as key arguments:" ] }, { "cell_type": "code", "execution_count": 11, "id": "01e3e613", "metadata": {}, "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", "
countmeanmax
Title
Jupyter Tutorial218103.530134.0
PyViz Tutorial14873.04873.0
Python Basics2261.0427.0
\n", "
" ], "text/plain": [ " count mean max\n", "Title \n", "Jupyter Tutorial 2 18103.5 30134.0\n", "PyViz Tutorial 1 4873.0 4873.0\n", "Python Basics 2 261.0 427.0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "evaluations[\"2021-12\"]" ] }, { "cell_type": "markdown", "id": "73b3f136", "metadata": {}, "source": [ "As before, a list of tuples with user-defined names can be passed:" ] }, { "cell_type": "code", "execution_count": 12, "id": "afcc870f", "metadata": {}, "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", "
2021-122022-01
MeanVarianceMeanVariance
Title
Jupyter Tutorial18103.5289465860.520505.5327142620.5
PyViz Tutorial4873.0NaN3930.0NaN
Python Basics261.055112.0251.01250.0
\n", "
" ], "text/plain": [ " 2021-12 2022-01 \n", " Mean Variance Mean Variance\n", "Title \n", "Jupyter Tutorial 18103.5 289465860.5 20505.5 327142620.5\n", "PyViz Tutorial 4873.0 NaN 3930.0 NaN\n", "Python Basics 261.0 55112.0 251.0 1250.0" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tuples = [(\"Mean\", \"mean\"), (\"Variance\", \"var\")]\n", "\n", "grouped[[\"2021-12\", \"2022-01\"]].agg(tuples)" ] }, { "cell_type": "markdown", "id": "4b664b9c", "metadata": {}, "source": [ "If we now assume that potentially different functions are to be applied to one or more of the columns, we pass a `dict` to `agg` that contains an assignment of column names to one of the function specifications:" ] }, { "cell_type": "code", "execution_count": 13, "id": "a6bc095c", "metadata": {}, "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", "
2021-122022-01
Title
Jupyter Tutorial18103.5327142620.5
PyViz Tutorial4873.0NaN
Python Basics261.01250.0
\n", "
" ], "text/plain": [ " 2021-12 2022-01\n", "Title \n", "Jupyter Tutorial 18103.5 327142620.5\n", "PyViz Tutorial 4873.0 NaN\n", "Python Basics 261.0 1250.0" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grouped.agg({\"2021-12\": \"mean\", \"2022-01\": \"var\"})" ] }, { "cell_type": "code", "execution_count": 14, "id": "a1bb27de", "metadata": {}, "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", "
2021-122022-01
minmaxmeanstdsum
Title
Jupyter Tutorial6073.030134.018103.517013.69626241011.0
PyViz Tutorial4873.04873.04873.0NaN3930.0
Python Basics95.0427.0261.0234.759451502.0
\n", "
" ], "text/plain": [ " 2021-12 2022-01\n", " min max mean std sum\n", "Title \n", "Jupyter Tutorial 6073.0 30134.0 18103.5 17013.696262 41011.0\n", "PyViz Tutorial 4873.0 4873.0 4873.0 NaN 3930.0\n", "Python Basics 95.0 427.0 261.0 234.759451 502.0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grouped.agg({\"2021-12\": [\"min\", \"max\", \"mean\", \"std\"], \"2022-01\": \"sum\"})" ] }, { "cell_type": "markdown", "id": "84e0f1ed-6bfe-4992-b2ce-aad260d2f37a", "metadata": {}, "source": [ "## Return aggregated data without row indices\n", "\n", "In all the examples so far, the aggregated data is returned with an index:" ] }, { "cell_type": "markdown", "id": "3a949c79-be37-4a4d-a9c9-64293749ac36", "metadata": {}, "source": [ "Since this is not always desired, you can disable this behaviour in most cases by passing `as_index=False` to `groupby`:" ] }, { "cell_type": "code", "execution_count": 15, "id": "8e04343e-f196-4862-b334-cf4447828c03", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2021-12 range 8131.000000\n", "2022-01 range 8543.000000\n", "2022-02 range 4490.666667\n", "dtype: float64" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grouped.agg([range]).mean()" ] }, { "cell_type": "markdown", "id": "41843bbf-9604-4b30-a42b-2acef4b890f1", "metadata": {}, "source": [ "Since this is not always desired, you can disable this behaviour by using `reset_index(drop=True)`:" ] }, { "cell_type": "code", "execution_count": 16, "id": "f8de28e4-85ee-4a30-add5-317da5f13d22", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 8131.000000\n", "1 8543.000000\n", "2 4490.666667\n", "dtype: float64" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grouped.agg([range]).mean().reset_index(drop=True)" ] } ], "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 }