Data Aggregation in Python

Python Tutorial 6.0

After learning to merge and appending in Python, let's now explore how to do aggregation in the data using Python. By now we have entered into an intermediate level of Python programming, few more articles and you would be ready to enter the arena of advance python.

But before entering into the dangerous combat arena with the furious statistical techniques, better learn all the basic moves !!!

Please download the .csv file that you can use to practice the codes.

import pandas as p



sample output : 


Only showing top 20 observations

Let's play our favorite game "Question and answer" to learn the concepts.

Q1. How many rows are there in the dataset?

Ans:  Aggregation['Product'].count()    # we can take  any column instead of product, the                                                               result would remain same 


Q2. How much is the total sales?

Ans:  Aggregation['Sales'].sum()


Q3. What is the total sale of 'APAC' Region?

Ans.  Aggregation['Sales'][(Aggregation.Region=='APAC')].sum() 


Q4. What is the total sale of Product 'A' in 'APAC' Region?

Ans. Aggregation['Sales'][(Aggregation.Product=='A')&(Aggregation.Region=='APAC') ].sum() 


Apart from count and sum, there are many functions which you can use to get basic statistic about the data. e. g.

mean, median, min, max, mode and std etc.

Q5. How many unique products are there?

Ans:  Aggregation['Product'].nunique()

Let's learn how to aggregate the data group wise

Q6. How to get a list of all distinct products?

Ans.  Aggregation.groupby(['Product']).groups.keys() 


Q7. Get sales by products

Ans. Aggregation.groupby('Product')['Sales'].sum()


Q8. Get sales by Products of 'APAC' Region only 

Ans.  Aggregation[Aggregation['Region']=='APAC'].groupby('Product')['Sales'].sum()


Let me just add one more dimension to it

Let's now aggregate the data using multiple variables with multiple measures. 

Q9. Create a table of total sales by Product and Region 

Ans.  Aggregation.groupby(['Product','Region'])['Sales'].sum()


Q10. Create a table of total sales and frequency by Product and Region 

Ans.  Aggregation.groupby(['Product','Region']).agg({'Sales':{'total_sales':'sum','Frequency':'count'}})


Whatever be the operation and calculation we have done in above examples either would create Panda Series or Panda Dataframe.

If we go theoretically then one column's output is called series and multiple column's output is called dataframe.

We can change it by a small change in syntax. Lets see how..

Q9. from above examples

a=Aggregation.groupby(['Product','Region'])['Sales'].sum()    # produce data series



Add only additional bracket to aggregated variable

a=Aggregation.groupby(['Product','Region'])[['Sales']].sum()    # produce data frame



One more concept I would like to tell you is

If you see the output of Q9 then you must have noticed that in Product column there are blank entries populated if a product is same. It is called indexing; an output is indexed by Product and Region. 

Since we may want to utilize this dataframe for further data processing, so it is good if we fill these blank entries.

To avoid this index(blank entries), pass “as_index=False” to the groupby operation.



and that's it for now! We have learned aggregation ... 

Humble appeal

Download our Android app 

Enjoy reading our other articles and stay tuned with us.

Kindly do provide your feedback in the 'Comments' Section and share as much as possible.


  1. To be a decent information researcher, it is critical to be curious, take a gander at information and determine bits of knowledge, spot inclines, etc. Basically, all exertion is aimed at realizing hierarchical change. data science course in pune

  2. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.
    business analytics course in delhi

  3. Attend The Machine Learning course in Bangalore From ExcelR. Practical Machine Learning course in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Machine Learning course in Bangalore.
    Machine Learning course in Bangalore

  4. Thank you so much for helping me out to find the Data analytics course in MumbaiOrganisations and introducing reputed stalwarts in the industry dealing with data analyzing & assorting it in a structured and precise manner. Keep up the good work. Looking forward to view more from you.

  5. Such a very useful article. I have learn some new information.thanks for sharing.
    data scientist course in mumbai


  6. Excelr is providing emerging & trending technology training, such as for data science, Machine learning, Artificial Intelligence, AWS, Tableau, Digital Marketing. Excelr is standing as a leader in providing quality training on top demanding technologies in 2019. Excelr`s versatile training is making a huge difference all across the globe. Enable ?business analytics? skills in you, and the trainers who were delivering training on these are industry stalwarts. Get certification on "
    data science course fees in hyderabad"
    and get trained with Excelr.

  7. I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article.
    Data science course in mumbai

  8. Nice Blog...Very interesting to read this article. I have learn some new information.thanks for sharing.
    ExcelR Mumbai

  9. Very nice blog here and thanks for post it.. Keep blogging...
    ExcelR data science training

  10. Good to become visiting your weblog again, it has been months for me. Nicely this article that i've been waited for so long. I will need this post to total my assignment in the college, and it has exact same topic together with your write-up. Thanks, good share.
    data analytics course hyderabad

  11. I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article.
    data science course in mumbai

  12. I am looking for and I love to post a comment that "The content of your post is awesome" Great work!
    data science course in mumbai

  13. Such a very useful article. I have learn some new information.thanks for sharing.
    data analytics courses


  14. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job!.... data science course Bangalore

  15. This post is very simple to read and appreciate without leaving any details out. Great work! data science courses

  16. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
    ExcelR Data Analytics Course
    Data Science Interview Questions


Do provide us your feedback, it would help us serve your better.