Skip to content

Commit 816079d

Browse files
committed
Updated Issue pandas-dev#18220
1 parent 4ae33be commit 816079d

File tree

2 files changed

+24
-70
lines changed

2 files changed

+24
-70
lines changed

doc/source/user_guide/groupby.rst

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -770,76 +770,6 @@ no column selection, so the values are just the functions.
770770
max_height="max",
771771
)
772772
773-
Passing a List of Tuples
774-
~~~~~~~~~~~~~~~~~~~~~~~~~
775-
776-
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.
777-
778-
Example:
779-
--------
780-
781-
Consider the following DataFrame `df`:
782-
783-
.. ipython:: python
784-
785-
import pandas as pd
786-
import numpy as np
787-
788-
df = pd.DataFrame({'key': ['a', 'a', 'b', 'b', 'a'],
789-
'data': np.random.randn(5)})
790-
791-
Suppose we want to group the DataFrame by the 'key' column and apply different aggregations to the 'data' column:
792-
793-
.. ipython:: python
794-
795-
result = df.groupby('key')['data'].agg([('foo', 'mean')])
796-
797-
In this example, the output column 'foo' contains the mean value of the 'data' column for each group.
798-
799-
To apply multiple aggregations to the same column, you can pass a list of tuples:
800-
801-
.. ipython:: python
802-
803-
result = df.groupby('key')['data'].agg([('col1', 'mean'), ('col2', 'std')])
804-
805-
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.
806-
807-
Similarly, you can extend this approach to include more aggregations:
808-
809-
.. ipython:: python
810-
811-
result = df.groupby('key')['data'].agg([('col1', 'mean'), ('col2', 'std'), ('col3', 'min')])
812-
813-
Here, the resulting DataFrame will have three columns: 'col1', 'col2', and 'col3', each containing the respective aggregation result for the 'data' column.
814-
815-
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:
816-
817-
.. ipython:: python
818-
819-
result = df.groupby('key')['data'].agg([('mean_value', 'mean'), ('median_value', 'median')])
820-
821-
The resulting DataFrame will have two columns: 'mean_value' and 'median_value', each containing the corresponding aggregation results.
822-
823-
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.
824-
825-
For a copy-pastable example, consider the following DataFrame `df`:
826-
827-
.. ipython:: python
828-
829-
import pandas as pd
830-
import numpy as np
831-
832-
df = pd.DataFrame({'key': ['a', 'a', 'b', 'b', 'a'],
833-
'data': np.random.randn(5)})
834-
835-
You can then use the `agg` function with a list of tuples for aggregations:
836-
837-
.. ipython:: python
838-
839-
result = df.groupby('key')['data'].agg([('foo', 'mean')])
840-
841-
This will create a DataFrame with the mean values for each group under the 'foo' column.
842-
843773
844774
Applying different functions to DataFrame columns
845775
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pandas/core/groupby/generic.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,30 @@ class DataFrameGroupBy(GroupBy[DataFrame]):
14471447
A
14481448
1 1.0
14491449
2 3.0
1450+
1451+
Passing a List of Tuples
1452+
1453+
Demonstrates using the `agg` method with a list of tuples for grouping and aggregation.
1454+
1455+
Consider a DataFrame `df`:
1456+
1457+
key | data
1458+
----|------
1459+
'a' | 0.5
1460+
'a' | -1.0
1461+
'b' | 2.0
1462+
'b' | -0.5
1463+
'a' | 1.2
1464+
1465+
Example: Applying multiple aggregations - mean and standard deviation
1466+
df_result = df.groupby('key')['data'].agg([('mean_value', 'mean'), ('std_deviation', 'std')])
1467+
1468+
Using a list of tuples provides a concise way to apply multiple aggregations to the same column while controlling
1469+
the output column names. This approach is especially handy when you need to calculate various statistics on
1470+
the same data within each group.
1471+
1472+
:return: Example of using the `agg` method with a list of tuples for grouping and aggregation.
1473+
:rtype: None
14501474
"""
14511475
)
14521476

0 commit comments

Comments
 (0)