Aggregation of fields is one of the basic necessity for data analysis and data science. Python’s Pandas module provide easy ways to do aggregation and calculate metrics. Finding minimum value for each group can also be achieved while doing the group by. The function that is helpful for finding the minimum value is min(). The below article explains with the help of an example How to calculate minimum value by Group in Python.
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 minimum revenue for each stores. As there are 4 months of data available for each store, there will be one minimum value out of the four.
Find the minimum sales for each store in Pandas
Step 1: Firstly, Import all the necessary modules.
import pandas as pd
Step 2: Use min() function along with groupby operation. As we are looking forward to group by each StoreID, by=”StoreID” works as groupby parameter. The Revenue field contains the sales of each store. To find the minimum value, we will be using “Revenue” for minimum value calculation. For the current example, syntax is:
df1.groupby(by="StoreID")["Revenue"].min()
Example 2: Calculate Minimum value for each Department
Here we are looking forward to calculate the minimum value across each department. So, the field in groupby operation will be “Department”
df1.groupby(by="Department")["Revenue"].min()
Thus, John is able to calculate values as per his requirement in Python.
To get top certifications in Python and build your resume visit here. Also, you can read books listed here to build strong knowledge around Python.
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.