Menu

Grouping Data to Find Patterns
📰
0

Grouping Data to Find Patterns

DEV Community·Akhilesh·about 1 month ago
#kIlDomej
Reading 0:00
15s threshold

You have 10,000 rows of sales data. You do not care about 10,000 rows. You care about one question. Which region had the highest average sale value last quarter? To answer that, you need to group all the rows by region, then calculate the average sale value within each group. That is groupby. And it is the operation that turns raw data into answers. The Simplest GroupBy import pandas as pd import numpy as np data = { " name " : [ " Alex " , " Priya " , " Sam " , " Jordan " , " Lisa " , " Ravi " , " Tom " , " Nina " ], " department " : [ " Engineering " , " Marketing " , " Engineering " , " Sales " , " Marketing " , " Engineering " , " Sales " , " Marketing " ], " salary " : [ 55000 , 82000 , 43000 , 95000 , 67000 , 71000 , 88000 , 74000 ], " years " : [ 2 , 5 , 1 , 8 , 4 , 3 , 6 , 3 ], " promoted " : [ False , True , False , True , True , False , True , False ] } df = pd . DataFrame ( data ) dept_avg_salary = df . groupby ( " department " )[ " salary " ].…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More