Advertisements
Advertisements
Question
Explain the concept of GROUP BY with help on an example.
Code Writing
Explain
Advertisements
Solution
GROUP BY is used to group data based on one or more columns and perform calculations like sum, count, average, minimum, and maximum on each group.
import pandas as pd
data = {'Department':['IT','HR','IT','HR'],
'Salary':[50000,40000,60000,45000]}
df = pd.DataFrame(data)
df.groupby('Department')['Salary'].sum()
Output:
| Department | |
| HR | 85000 |
| IT | 110000 |
| Name | Salary, dtype: int64 |
shaalaa.com
Is there an error in this question or solution?
