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

Pandas: Summary Statistics

Python library

Aggregation means combining multiple data items from the data set into a single value. Several commonly used functions like sum(), mean(), avg() are used for aggregation of data.

Summary Statistics

•  Aggregation means combining multiple data items from the data set into a single value. Several commonly used functions like sum(), mean(), avg() are used for aggregation of data.

• Pandas provide various library functions to perform aggregation operations. Some of the functions used in aggregation are as follows ‒

sum() ‒ Compute sum of column values

max() ‒ Compute max of column values

size() ‒ Compute column sizes

first() ‒ Compute first of group values

count() ‒ Compute count of column values

var() ‒ Compute variance of column

min() ‒ Compute min of column values

mean() ‒ Compute mean of column

describe() ‒ Generates descriptive statistics

last() ‒ Compute last of group values

std() ‒ Standard deviation of column

sem() ‒ Standard error of the mean of column

 

For example ‒

•  The sum() function calculates the sum of every value. We can find the total weights of females and total weights of males using sum() function. The code is as follows ‒

In [4]: import pandas as pd

data = {'Gender': ['f', 'm', 'f', 'm', 'm', 'f', 'm'], 'Weight : [45,71,69,73,80,55,98]}

df = pd.DataFrame(data)

print(df)

f = df[ 'Gender'] = = 'f'

female wt = df[f]['Weight'].sum()

print("‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒")

print("Total weight of all females is: ", female_wt)

m = df['Gender'] = = 'm'

male wt ‒ df[m]['weight'].sum()

print("Total weight of all males is: ",male_wt)

Output


• The describe() function gives the summary of our data set. The code is

In [5]: import pandas as pd

data = {'Gender': ['f', 'm', 'f', , 'm', 'm', 'f', 'm'], 'Weight':[45,71,69,73,80,55,98]}

df = pd.DataFrame(data)

print(df)

df.describe()

Output


• The agg() method allows to apply a function or a list of function names to be executed. The agg() method is an alias of the aggregate() method. The agg() function is used to calculate sum, min and max of each column in the data set.

• For example ‒

import pandas as pd

data = {'Gender': ['f','m','f', 'm','m', 'f','m'], 'Weight': [45,71,69,73,80,55,98]}

df = pd.DataFrame(data)

print(df)

df.agg(['sum','min','max'])

Output



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


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