English

Explain the concept of GROUP BY with help on an example.

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?
Chapter 3: Data Handling using Pandas - II - Exercise [Page 102]

APPEARS IN

NCERT Informatics Practices [English] Class 12
Chapter 3 Data Handling using Pandas - II
Exercise | Q 9. | Page 102
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×