pandas: how to group by multiple columns and perform different aggregations on multiple columns?

pandas groupby aggregate multiple columns
pandas groupby multiple columns
pandas groupby sum multiple columns
pandas groupby multiple aggregations on different columns
pandas groupby aggregate to list
pandas groupby count
pandas groupby agg lambda
groupby without aggregation pandas

Lets say I have a table that look like this:

Company      Region     Date           Count         Amount
AAA          XXY        3-4-2018       766           8000
AAA          XXY        3-14-2018      766           8600
AAA          XXY        3-24-2018      766           2030
BBB          XYY        2-4-2018        66           3400
BBB          XYY        3-18-2018       66           8370
BBB          XYY        4-6-2018        66           1380

I want to get rid of the Date column, then aggregate by Company AND region to find the average of Count and sum of Amount.

Expected output:

Company      Region     Count         Amount
AAA          XXY        766           18630
BBB          XYY        66            13150

I looked into this post here, and many other posts online, but seems like they are only performing one kind of aggregation action (for example, I can aggregate by multiple columns but can only produce one column output as sum OR count, NOT sum AND count)

Rename result columns from Pandas aggregation ("FutureWarning: using a dict with renaming is deprecated")

Can someone help?

What I did:

I followed this post here:

however, when i try to use the method presented in this article (toward the end of the article), by using dictionary:

aggregation = {
    'Count': {
        'Total Count': 'mean'
    'Amount': {
        'Total Amount': 'sum'

I would get this warning:

FutureWarning: using a dict with renaming is deprecated and will be removed in a future version
  return super(DataFrameGroupBy, self).aggregate(arg, *args, **kwargs)

I know it works now but i want to make sure my script works later too. How can I update my code to be compatible in the future?

Need aggregate by single non nested dictionary and then rename columns:

aggregation = {'Count':  'mean', 'Amount': 'sum'}
cols_d = {'Count': 'Total Count', 'Amount': 'Total Amount'}

df = df.groupby(['Company','Region'], as_index=False).agg(aggregation).rename(columns=cols_d)
print (df)
  Company Region  Total Count  Total Amount
0     AAA    XXY          766         18630
1     BBB    XYY           66         13150

Another solution with add_prefix instead rename:

aggregation = {'Count':  'mean', 'Amount': 'sum'}
df = df.groupby(['Company','Region']).agg(aggregation).add_prefix('Total ').reset_index()
print (df)
  Company Region  Total Count  Total Amount
0     AAA    XXY          766         18630
1     BBB    XYY           66         13150

pandas: how to group by multiple columns and perform different , Need aggregate by single non nested dictionary and then rename columns: aggregation = {'Count': 'mean', 'Amount': 'sum'} cols_d = {'Count':  Group and Aggregate by One or More Columns in Pandas. June 01, 2019 . Pandas comes with a whole host of sql-like aggregation functions you can apply when grouping on one or more columns. This is Python’s closest equivalent to dplyr’s group_by + summarise logic. Here’s a quick example of how to group on one or multiple columns and summarise data with aggregation functions using Pandas.

df.groupby(['Region', 'Company']).agg({'Count': 'mean', 'Amount': 'sum'}).reset_index()


  Region Company  Count  Amount
0    XXY     AAA    766   18630
1    XYY     BBB     66   13150

Summarising, Aggregating, and Grouping data in Python Pandas , Aggregation and grouping of Dataframes is accomplished in Python Pandas using Groupby essentially splits the data into different groups depending on a keys are used to specify the columns upon which you'd like to perform operations, To apply multiple functions to a single column in your grouped data​, expand the  What if I told you that we can derive effective and impactful insights from our dataset in just a few lines of code? That’s the beauty of Pandas’ GroupBy function! I have lost count of the number of times I’ve relied on GroupBy to quickly summarize data and aggregate it in a way that’s easy

Try this:


Group By: split-apply-combine, Aggregation: computing a summary statistic (or statistics) about each group. to return a sensibly combined result if it doesn't fit into either of the above two categories GroupBy will tab complete column names (and other attributes) created, several methods are available to perform a computation on the grouped data. This was the second episode of my pandas tutorial series. I hope now you see that aggregation and grouping is really easy and straightforward in pandas… and believe me, you will use them a lot! Note: If you have used SQL before, I encourage you to take a break and compare the pandas and the SQL methods of aggregation.

'groupby' multiple columns and 'sum' multiple columns with different , 'groupby' multiple columns and 'sum' multiple columns with different types #​13821. Closed from decimal import * import pandas as pd df = pd.DataFrame( I'm assuming it gets excluded as a non-numeric column before any aggregation occurs. You can see You can't perform that action at this time. Pandas object can be split into any of their objects. There are multiple ways to split an object like − obj.groupby('key') obj.groupby(['key1','key2']) obj.groupby(key,axis=1) Let us now see how the grouping objects can be applied to the DataFrame object. Example

Multiple Aggregations in Pandas, Perform multiple aggregate functions simultaneously with Pandas 0.25. summer, added an easier way to do multiple aggregations on multiple columns. like get the same few summary stats from a bunch of different columns. You can group a Pandas DataFrame by a single column, or a list of columns  Using the as_index parameter while Grouping data in pandas prevents setting a row index on the result. Multiple Statistics per Group. The final piece of syntax that we’ll examine is the “agg()” function for Pandas. The aggregation functionality provided by the agg() function allows multiple statistics to be calculated per group in one calculation.

Pandas' groupby explained in detail, Learn how to master all Pandas' groupby functionalities, like The data set consists, among other columns, of fictitious sales reps, order The same logic applies when we want to group by multiple columns or Please note that agg and aggregate can be used interchangeably. agg Make Medium yours. By using ngroup(), we can extract information about the groups in a way similar to factorize() (as described further in the reshaping API) but which applies naturally to multiple columns of mixed type and different sources. This can be useful as an intermediate categorical-like step in processing, when the relationships between the group rows

  • please post your expected output as well.
  • @HaleemurAli added!