Aggregation of fields is one of the basic necessity for data analysis and data science. Pyspark provide easy ways to do aggregation and calculate metrics. Finding sum value for each group can also be achieved while doing the group by. The function that is helpful for finding the sum value is sum(). The below article explains with the help of an example How to sum by Group in Pyspark.
John has store sales data available for analysis. There are five columns present in the data, Geography (country of store), Department (Industry category of the store), StoreID (Unique ID of each store), Time Period (Month of sales), Revenue (Total Sales for the month). John is looking forward to calculate sum revenue for each stores. As there are 4 months of data available for each store, there will be one sum value out of the four.
Find the Total sales for each store in Pyspark
Step 1: Firstly, Import all the necessary modules.
import pandas as pd
import findspark
findspark.init()
import pyspark
from pyspark import SparkContext
from pyspark.sql import SQLContext
sc = SparkContext("local", "App Name")
sql = SQLContext(sc)
Step 2: Use sum() function along with groupby operation. As we are looking forward to group by each StoreID, “StoreID” works as groupby parameter. The Revenue field contains the sales of each store. To find the sum total value, we will be using “Revenue” for sum total value calculation. For the current example, syntax is:
Thus, John is able to calculate value as per his requirement in Pyspark. This kind of extraction can be a requirement in many scenarios and use cases. This example talks about one of the use case.
To get top certifications in Pyspark and build your resume visit here. Additionally, you can read books listed here to build strong knowledge around Pyspark.
Visit us below for video tutorial:
đŹ Stay Ahead in Data Science & AI â Subscribe to Newsletter!
đŻ Interview Series: Curated questions and answers for freshers and experienced candidates.
đ Data Science for All: Simplified articles on key concepts, accessible to all levels.
đ¤ Generative AI for All: Easy explanations on Generative AI trends transforming industries.
đĄ Why Subscribe? Gain expert insights, stay ahead of trends, and prepare with confidence for your next interview.
hey there and thanks on your info ?Ićłe definitely picked up something new from proper here. I did however experience some technical issues the usage of this site, since I skilled to reload the site lots of times prior to I may get it to load correctly. I were wondering if your web hosting is OK? Not that I’m complaining, but sluggish loading instances occasions will sometimes have an effect on your placement in google and can injury your high quality rating if advertising and ***********|advertising|advertising|advertising and *********** with Adwords. Well IćŚ including this RSS to my email and could glance out for much extra of your respective fascinating content. Make sure you replace this once more soon..
My wife and i were absolutely contented when Louis could deal with his preliminary research by way of the precious recommendations he acquired through the web pages. It’s not at all simplistic to just possibly be giving out information and facts which often some others have been trying to sell. And we consider we’ve got the blog owner to be grateful to because of that. The most important illustrations you have made, the easy blog navigation, the relationships you will give support to create – it is everything amazing, and it’s helping our son and the family know that the issue is amusing, and that is pretty serious. Thanks for the whole lot!
I truly appreciate this post. I have been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thx again
Aw, this was a really nice post. In thought I would like to put in writing like this additionally ?taking time and precise effort to make an excellent article?but what can I say?I procrastinate alot and not at all seem to get something done.
hey there and thanks on your info ?Ićłe definitely picked up something new from proper here. I did however experience some technical issues the usage of this site, since I skilled to reload the site lots of times prior to I may get it to load correctly. I were wondering if your web hosting is OK? Not that I’m complaining, but sluggish loading instances occasions will sometimes have an effect on your placement in google and can injury your high quality rating if advertising and ***********|advertising|advertising|advertising and *********** with Adwords. Well IćŚ including this RSS to my email and could glance out for much extra of your respective fascinating content. Make sure you replace this once more soon..
My wife and i were absolutely contented when Louis could deal with his preliminary research by way of the precious recommendations he acquired through the web pages. It’s not at all simplistic to just possibly be giving out information and facts which often some others have been trying to sell. And we consider we’ve got the blog owner to be grateful to because of that. The most important illustrations you have made, the easy blog navigation, the relationships you will give support to create – it is everything amazing, and it’s helping our son and the family know that the issue is amusing, and that is pretty serious. Thanks for the whole lot!
I truly appreciate this post. I have been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thx again
Aw, this was a really nice post. In thought I would like to put in writing like this additionally ?taking time and precise effort to make an excellent article?but what can I say?I procrastinate alot and not at all seem to get something done.