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

Aggregation=p.read_csv('C:\\data.csv')


Aggregation


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 



Output:








Q2. How much is the total sales?

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



Output:







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


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




Output:



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


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




Output:



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() 


Output:









Q7. Get sales by products


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

Output:















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

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


Output:








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()


Output:


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'}})

Output:





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

type(a)


Output:





Add only additional bracket to aggregated variable


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

type(a)


Output:













One more concept I would like to tell you is
indexing


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.


a=Aggregation.groupby(['Region','Product'],as_index=False)[['Sales']].sum()
a


Output:


















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.

67 comments:

  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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.

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

    ReplyDelete

  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.

    ReplyDelete
  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

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

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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

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

    ReplyDelete

  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

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

    ReplyDelete
  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

    ReplyDelete
  17. Grow your career with Python in machine learning. High technologies solutions is the best Python training institute in Delhi and Noida with 100% placement help. 5+ years experienced trainers.Join now!! Call at +919311002620.
    Python with machine learning training in delhi
    Python with machine learning training in Noida

    ReplyDelete
  18. keep up the good work. this is an Ossam post. This is to helpful, i have read here all post. i am impressed. thank you. this is our data analytics course mumbai
    data analytics course mumbai | https://www.excelr.com/data-analytics-certification-training-course-in-mumbai

    ReplyDelete
  19. This is a wonderful article, Given so much info in it, Thanks for sharing. CodeGnan offers courses in new technologies and makes sure students understand the flow of work from each and every perspective in a Real-Time environmen python training in vijayawada. , data scince training in vijayawada . , java training in vijayawada. ,

    ReplyDelete
  20. This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.

    Dental care for kids

    ReplyDelete
  21. This is a wonderful article, Given so much info in it, Thanks for sharing. CodeGnan offers courses in new technologies and makes sure students understand the flow of work from each and every perspective in a Real-Time environmen python training in vijayawada. , data scince training in vijayawada . , java training in vijayawada. ,

    ReplyDelete
  22. Impressive! I finally found a great post here. Nice article on data science . It's really a nice experience to read your post. Thanks for sharing your innovative ideas to our vision.
    Data Science Course
    Data Science Course in Marathahalli
    Data Science Course Training in Bangalore

    ReplyDelete
  23. The information provided on the site is informative. Looking forward for more such blogs. Thanks for sharing .
    Artificial Inteligence course in Delhi
    AI Course in Delhi

    ReplyDelete

  24. The blog is very informative.Keep posting like this.
    Aviation academy in chennai

    ReplyDelete
  25. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
    Data science Interview Questions

    ReplyDelete
  26. I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article.

    ExcelR data analytics courses

    ReplyDelete
  27. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care
    http://chennaitraining.in/qliksense-training-in-chennai/
    http://chennaitraining.in/pentaho-training-in-chennai/
    http://chennaitraining.in/machine-learning-training-in-chennai/
    http://chennaitraining.in/artificial-intelligence-training-in-chennai/
    http://chennaitraining.in/snaplogic-training-in-chennai/
    http://chennaitraining.in/snowflake-training-in-chennai/

    ReplyDelete
  28. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
    Data science Interview Questions
    Data Science Course

    ReplyDelete
  29. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries. keep it up.
    data analytics course in Bangalore

    ReplyDelete
  30. Attend The Bangalore Digital Marketing Course From ExcelR. Practical Bangalore Digital Marketing Course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Bangalore Digital Marketing Course.
    Bangalore Digital Marketing Course

    ReplyDelete
  31. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    Correlation vs Covariance

    ReplyDelete
  32. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    Correlation vs Covariance
    Simple linear regression

    ReplyDelete
  33. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!

    Simple Linear Regression

    ReplyDelete
  34. This was really one of my favorite website. Please keep on posting. ExcelR Data Science Course In Pune

    ReplyDelete
  35. Attend The Data Science Courses From ExcelR. Practical Data Science Courses Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Science Courses.
    Data Science Courses

    ReplyDelete
  36. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    Correlation vs Covariance
    Simple linear regression
    data science interview questions

    ReplyDelete
  37. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    Correlation vs Covariance
    Simple linear regression
    data science interview questions

    ReplyDelete
  38. This Was An Amazing ! I Haven't Seen This Type of Blog Ever ! Thankyou For Sharing, data science certification

    ReplyDelete
  39. Superb Information, I really appreciated with it, This is fine to read and valuable pro potential, I really bookmark it, pro broaden read. Appreciation pro sharing. I like it. ExcelR Data Scientist Course In Pune

    ReplyDelete
  40. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.

    Simple Linear Regression

    Correlation vs Covariance

    ReplyDelete

  41. This post is really helpful for us. I certainly love this website, keep on it.
    Data Science Course in Hyderabad

    ReplyDelete
  42. Very interesting blog. Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome.You can also check my articles as well.

    Data Science In Banglore With Placements
    Data Science Course In Bangalore
    Data Science Training In Bangalore
    Best Data Science Courses In Bangalore
    Data Science Institute In Bangalore

    Thank you..

    ReplyDelete
  43. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. machine learning projects for final year In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete
  44. Avail Best Data Science Course in Bangalore and Kick Start Your Career as a Successful Data Scientist in Bangalore within 4 months. Data Science Course in Bangalore | Data Scientist Training in Bangalore with Placements. Learn the advanced data science course concepts and get your skills upgraded from the pioneers in Data Science.
    Online Data Science Course in Bangalore

    ReplyDelete
  45. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    Correlation vs Covariance
    Simple linear regression
    data science interview questions

    ReplyDelete
  46. I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article.

    Simple Linear Regression

    Correlation vs Covariance

    ReplyDelete
  47. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    Correlation vs Covariance
    Simple linear regression
    data science interview questions

    ReplyDelete
  48. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing
    best data analyst courses in mumbai

    ReplyDelete
  49. Thanks for sharing great information. I like your blog and highly recommend. We also offer best data science training in Hyderabaddata scientist courses

    ReplyDelete
  50. Attend The Data Analyst Course From ExcelR. Practical Data Analyst Course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analyst Course.
    Data Analyst Course

    ReplyDelete
  51. This Was An Amazing ! I Haven't Seen This Type of Blog Ever ! Thankyou For Sharing, data sciecne course in hyderabad

    ReplyDelete
  52. Amazing Article ! I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    Simple Linear Regression
    Correlation vs covariance
    data science interview questions
    KNN Algorithm

    ReplyDelete
  53. Amazing post found to be very impressive while going through this post. Thanks for sharing and keep posting such an informative content. data science courses

    ReplyDelete

  54. You might comment on the order system of the blog. You should chat it's splendid. Your blog audit would swell up your visitors. I was very pleased to find this site.I wanted to thank you for this great read!!
    Data Analyst Course

    ReplyDelete
  55. Impressive blog with lovely information. really very useful article for us thanks for sharing such a wonderful blog. data science training in Hyderabad

    ReplyDelete

  56. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. ExcelR Data Science Course In Pune Any way I’ll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info.

    ReplyDelete
  57. Found your post interesting to read. I cant wait to see your post soon. Good Luck for the upcoming update. This article is really very interesting and effective, data science courses

    ReplyDelete
  58. Attend The Data Analyst Course From ExcelR. Practical Data Analyst Course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analyst Course.
    Data Analyst Course

    ReplyDelete
  59. Amazing Article ! I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    Simple Linear Regression
    Correlation vs covariance
    data science interview questions
    KNN Algorithm
    Logistic Regression explained

    ReplyDelete
  60. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.data science training in Hyderabad

    ReplyDelete

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