Skip to content

Commit df7a885

Browse files
committed
Added a missing documentation on an existing behavior of aggregation.
1 parent cb283d9 commit df7a885

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

doc/source/user_guide/groupby.rst

+54
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,60 @@ no column selection, so the values are just the functions.
799799
max_height="max",
800800
)
801801
802+
803+
Passing a List of Tuples
804+
~~~~~~~~~~~~~~~~~~~~~~~~~
805+
806+
Instead of a dictionary, you can also pass a list of tuples to the `agg` method to achieve similar results. Each tuple contains the output column name as the first element and the aggregation function as the second element. This approach is particularly useful for applying multiple aggregations on the same column.
807+
808+
Example:
809+
--------
810+
811+
Consider the following DataFrame `df`:
812+
813+
.. ipython:: python
814+
815+
import pandas as pd
816+
import numpy as np
817+
818+
df = pd.DataFrame({'key': ['a', 'a', 'b', 'b', 'a'],
819+
'data': np.random.randn(5)})
820+
821+
Suppose we want to group the DataFrame by the 'key' column and apply different aggregations to the 'data' column:
822+
823+
.. ipython:: python
824+
825+
result = df.groupby('key')['data'].agg([('foo', 'mean')])
826+
827+
In this example, the output column 'foo' contains the mean value of the 'data' column for each group.
828+
829+
To apply multiple aggregations to the same column, you can pass a list of tuples:
830+
831+
.. ipython:: python
832+
833+
result = df.groupby('key')['data'].agg([('col1', 'mean'), ('col2', 'std')])
834+
835+
In this case, the resulting DataFrame will have two columns: 'col1' containing the mean and 'col2' containing the standard deviation of the 'data' column for each group.
836+
837+
Similarly, you can extend this approach to include more aggregations:
838+
839+
.. ipython:: python
840+
841+
result = df.groupby('key')['data'].agg([('col1', 'mean'), ('col2', 'std'), ('col3', 'min')])
842+
843+
Here, the resulting DataFrame will have three columns: 'col1', 'col2', and 'col3', each containing the respective aggregation result for the 'data' column.
844+
845+
In addition to the examples above, let's consider a scenario where we want to calculate both the mean and the median of the 'data' column for each group:
846+
847+
.. ipython:: python
848+
849+
result = df.groupby('key')['data'].agg([('mean_value', 'mean'), ('median_value', 'median')])
850+
851+
The resulting DataFrame will have two columns: 'mean_value' and 'median_value', each containing the corresponding aggregation results.
852+
853+
Using a list of tuples provides a concise way to apply multiple aggregations to the same column while controlling the output column names. This approach is especially handy when you need to calculate various statistics on the same data within each group.
854+
855+
802856
Applying different functions to DataFrame columns
803857
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
804858

0 commit comments

Comments
 (0)