For list of Pandas tutorials click here...
https://www.nbshare.io/notebooks/pandas/
To group a Pandas DataFrame by multiple columns and apply multiple custom aggregate functions to multiple columns, you can use the groupby method of the DataFrame and the apply method of the resulting GroupBy object. Here's an example of how you could do this:
Let us first create a simple Pandas Dataframe.
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 2, 3, 3, 3],
'B': [10, 20, 20, 30, 30, 40],
'C': [100, 200, 200, 300, 300, 400]})
print(df)
A B C 0 1 10 100 1 2 20 200 2 2 20 200 3 3 30 300 4 3 30 300 5 3 40 400
Let us define our custom aggregate functions.
# Define custom aggregate functions
def custom_mean(x):
return x.mean()
def custom_sum(x):
return x.sum()
The above two functions are pretty much self explanatory. So let us now apply the custom aggregate functions to our columns as shown below. This will group the DataFrame by columns A and B, and for each group it will apply the custom functions custom_mean and custom_sum to the column C. The resulting DataFrame will have the following output:
# Group the DataFrame by columns 'A' and 'B' and apply the custom functions
result = df.groupby(['A', 'B']).apply(lambda x: pd.Series({'mean_C': custom_mean(x['C']),
'sum_C': custom_sum(x['C'])}))
print(result)
mean_C sum_C A B 1 10 100.0 100.0 2 20 200.0 400.0 3 30 300.0 600.0 40 400.0 400.0
You can also use the agg method of the GroupBy object to apply multiple aggregate functions to multiple columns at once:
# Group the DataFrame by columns 'A' and 'B' and apply the custom aggregation functions to columns 'C'.
df.groupby(['A', 'B']).agg({'C': ['mean', 'sum']})
C | |||
---|---|---|---|
mean | sum | ||
A | B | ||
1 | 10 | 100.0 | 100 |
2 | 20 | 200.0 | 400 |
3 | 30 | 300.0 | 600 |
40 | 400.0 | 400 |
Note above group 3 contains two rows.
Related Notebooks
- Return Multiple Values From a Function in Python
- Pandas Groupby Count of Rows In Each Group
- What is LeakyReLU Activation Function
- How To Add Regression Line On Ggplot
- How To Install Python TensorFlow On Centos 8
- How To Install R Sparklyr H2O Tensorflow Keras On Centos
- 3 Ways to Rename Columns in Pandas DataFrame
- Select Pandas Dataframe Rows And Columns Using iloc loc and ix
- How To Drop One Or More Columns In Pandas Dataframe