Python for Data Science: Chapter 5: NumPy and Pandas Libraries

Pandas: GroupBy Object

Python library

Pandas groupby is used for grouping the data according to the categories and applying a function to the categories.

GroupBy Object

• Pandas groupby is used for grouping the data according to the categories and applying a function to the categories. It also aggregates the data efficiently.

• The syntax is as follows ‒

DataFrame.groupby(by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True, squeeze=False, **kwargs)

Parameters:

•  by: Mapping, function, str or iterable

•  axis: int, default 0

•  level: If the axis is a MultiIndex (hierarchical), group by a particular level or levels

•  as_index: For aggregated output, return object with group labels as the index. Only relevant for DataFrame input. as_index=False is effectively "SQL‒style" grouped output

•  sort: Sort group keys. Get better performance by turning this off. Note this does not influence the order of observations within each group. groupby preserves the order of rows within each group.

•  group_keys: When calling apply, add group keys to index to identify pieces

•  squeeze: Reduce the dimensionality of the return type if possible, otherwise return a consistent type

Returns: GroupBy object

•  For example ‒ Following code shows how to apply groupby function on some column name. We can provide any number of columns to Groupby function.

import pandas as pd

spending =

pd.DataFrame({'Year': [1970, 1970, 1970, 1970, 1970, 1971, 1971, 1971, 1971, 1971, 1972, 1972, 1972],

'Country': ['Germany', 'France', 'Great Britain', 'Japan', 'USA','Canada',

'Germany','Great Britain', 'Japan', 'USA', 'Germany', 'Japan','USA'],

'Spending USD': [252,192,123,150,326,313,298,134,163,357,337,185,397],

'Life Expectancy': [70,72,71,72,71,73,70,72,73,71,71,74,71]},

columns=['Year','Country','Spending_USD",'LifeExpectancy']) print(spendings.groupby(['Country']))

Output

<pandas.core.groupby.generic.DataFrameGroupBy object at 0x000002B50B4BB8D0>

Thus the GroupBy function returns the groupby object.

 

Python for Data Science: Chapter 5: NumPy and Pandas Libraries : Tag: Computer Programming, Python, Data Science : Python library - Pandas: GroupBy Object


Python for Data Science: Chapter 5: NumPy and Pandas Libraries



Under Subject


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science - Laboratory

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation