Learn Python

Learn Data Structure & Algorithm

Learn Numpy

Pandas Introduction

Pandas Series

Pandas DataFrame

Pandas Read Files

Pandas Some functions and properties

Pandas Math Function

Pandas Selection

Pandas Change Type

Pandas Concatenate & Split

Pandas Sorting

Pandas Filter

Pandas Data Cleaning

Pandas Group by

Pandas Time Series

Pandas Analysis 1

Pandas Analysis 2

Pandas Analysis 3

Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Pandas math related functions

Let's see the dataset. This dataset is will be used in the upcoming example of code.

Input
import pandas as pd
df=pd.read_csv("practice.csv")
print(df)
Output
Id Name Group_name Total_marks Grade Ranking
0 01 A Science 700 A+ 01
1 02 B Commerce 618 B+ 02
2 03 A Science 700 A+ 01
3 04 D Arts 687 A+ 01
4 05 E Commerce 611 B+ 02
5 06 F Arts 599 C+ 03
6 07 P Science 575 C+ 03
7 08 F Arts 600 C 03
8 09 I Commerce 550 C+ 03
9 10 J Science 650 A+ 01
10 11 K Arts 680 A+ 01
11 12 L Science 570 C+ 03
12 13 M Arts 599 C+ 03
13 14 N Commerce 597 C+ 03
14 15 O Science 697 A+ 01
15 16 B Arts 570 C+ 03
16 17 D Science 588 C+ 03
17 18 E Science 687 A+ 01
18 19 C Commerce 688 A+ 01
19 20 P Arts 588 C+ 03
20 21 C Science 619 B+ 02
21 22 M Commerce 600 B+ 02
22 23 P Arts 700 A+ 01

sum() function

The sum () function is used to get the sum of the numeric column.

Input
import pandas as pd
df=pd.read_csv("practice.csv")
df1=df.Total_marks.sum()
print(df1)
Output
14473

max() function

The max() function is used to get the maximum value of the numeric column.

Input
import pandas as pd
df=pd.read_csv("practice.csv")
df1=df.Total_marks.max()
print(df1)
Output
700

min() function

The min() function is used to get the minimum value of the numeric column.

Input
import pandas as pd
df=pd.read_csv("practice.csv")
df1=df.Total_marks.min()
print(df1)
Output
550

mean() function

The mean() function is used to get the mean value of numeric columns.

Example 1:

Input
import pandas as pd
df=pd.read_csv("practice.csv")
df1=df.mean()
print(df1)
Output
Id    12.000000
Total_marks    629.260870
Ranking    .043478
dtype: float64

Example 2:

Input
import pandas as pd
df=pd.read_csv("practice.csv")
df1=df.mean()
print(df1)
Output
629.2608695652174

median() function

The median() function is used to get the median value of numeric columns.

Example 1:

Input
import pandas as pd
df=pd.read_csv("practice.csv")
df1=df.median()
print(df1)
Output
Id    12.0
Total marks    600.0
Ranking    2.0
dtype: float64

Example 2:

Input
import pandas as pd
df=pd.read_csv("practice.csv")
df1=df["Total marks"].median()
print(df1)
Output
600.0

mode() function

The mode() function is used to get the mode value of the categorical column.

Input
import pandas as pd
df=pd.read_csv("D:\\CSV Datasets for practice\\practice.csv")
df1=df.Name.mode()
print(df1)
Output
0   Science
dtype: object

count() function

The count() function is used to get the count of numeric columns.

Example 1:

Input
import pandas as pd
df=pd.read_csv("D:\\CSV Datasets for practice\\practice.csv")
df1=df.count()
print(df1)
Output
Id        23
Name         23
Group_name     23
Total marks     23
Grade         23
Ranking         23
Date         23 dtype: int64

Example 2:

Input
import pandas as pd
df=pd.read_csv("D:\\CSV Datasets for practice\\practice.csv")
df1=df["Total marks"].count()
print(df1)
Output
23

var() function

The var() function is used to get the var of numeric columns.

Example 1:

Input
import pandas as pd
df=pd.read_csv("practice.csv")
df1=df.var()
print(df1)
Output
Id     46.000000
Total marks     2703.703557
Ranking     0.861660
dtype: float64

Example 2:

Input
import pandas as pd
df=pd.read_csv("practice.csv")
df1=df["Total marks"].var()
print(df1)
Output
2703.7035573122535

std() function

Std() function is used to get the mean value of numeric columns.

Example 1:

Input
import pandas as pd
df=pd.read_csv("D:\\CSV Datasets for practice\\practice.csv")
df1=df.std()
print(df1)
Output
Id     6.782330
Total_marks  51.136375
Ranking     0.928256
dtype: float64

Example 2:

Input
import pandas as pd
df=pd.read_csv("D:\\CSV Datasets for practice\\practice.csv")
df1=df.std()
print(df1)
Output
51.13637505489552

aggregate() function

Using aggregate() function we can apply some aggregation like sum, max, mix etc across one or more column. Aggregation only can be applyed in numerical column.

Syntax:
DataFrame.aggregate(func, axis=0, *args, **kwargs)
Here,
function= string, dictionary, or list of string/callables.
axis= value 0(row) or 1(column)

Let's aggregate sum and max function across all the columns in data frame.

In the dataset we have numerical and categorical columns. We know that aggregation only applied in numerical column. Because we are running aggregation on the whole dataset, so we are not defining any column name in the function but aggregate function will only take numerical columns for work. So we will see outputs for all the numerical columns present in the dataset.

Example 1:

Input
import pandas as pd
df.aggregate(['sum', 'max']) Output

Id Total_marks Ranking
sum 279 14473 47
max 23 700 3

Let's apply different aggregation functions across different columns.

Example 1:

Input
import pandas as pd

df.aggregate({"Id":['sum', 'min'],
    "Total_marks":['max', 'min'],
    "Ranking":['min', 'sum']})
Output

Id Total_marks Ranking
sum 279 NaN 47
max NaN 700 NaN
min 1 550 1

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.