Data Science with Python: NumPy, Pandas, Matplotlib
Data Science with Python: NumPy, Pandas, Matplotlib
Python (NAI4501)
Module-3: Introduction To NumPy, Pandas and
Matplotlib
S
.R
Dr
2 5/
20
Module-3 has been prepared and compiled by Dr. Radhey Shyam, with grateful acknowledgment to those who made their course contents
freely available or (Contributed directly or indirectly). Feel free to use this study material for your own academic purposes. For any query,
gu
S
Identify appropriate data visualization techniques given particular
CO2
requirements imposed by the data together with the driving questions.
.R
Build data graphics with the appropriate data visualization and analytics
CO3
software for the task at hand.
Student must be able to preprocess the data using cleaning, integration,
CO4
Dr
transformation and find correlations among the data.
5/
Contact Mapped
Module Course Contents
Hrs. CO
2
Introduction to Data Science Case for data science, data
science classification, data science algorithms; Data Science
20
30 CO2,
2 animation, Visualizing locations, Visualizing time Building
Hours CO3
gu
Suggested Readings
1. Data Science Fundamentals and Practical Approaches: Understand Why Data Science Is the Next
by Dr Gypsy Anand/ Dr Rupam Sharma.
2. Wes McKinney. “Python for Data Analysis”, O'Reilly Media, 2017, 2nd Edition.
3. Grus, Joel. “Data Science from Scratch: First Principles with Python”, O'Reilly Media, 2015, 1st
Edition.
4. Brian K. Jones and David M. Beazley. “Python Cookbook”, O'Reilly Media, 2013, 3rd Edition.
5. Dipanjan Sarkar, “Text Analytics with Python: A Practitioner‟s Guide to Natural Language
Processing”, A Press, 2019, 2nd Edition.
6. Daniel Jurafsky, James H. Martin, “Speech and Language Processing”, Pearson, 2009.
Online Resources
1. [Link]
S
CO3 1 1 2 1 3 2 2 2
CO4 1 2 2 2 1 1 1 1
.R
Dr
2 5/
, 20
10
st
gu
Au
1 Introduction
In the Python data science ecosystem, NumPy, Pandas, and Matplotlib form the core stack for:
S
Goal: Understand the purpose, features, and usage of each library to prepare for real-world data
analysis tasks.
.R
2 NumPy – Numerical Python
What is NumPy?
Dr
5/
A library for numerical computations using arrays.
2
Provides fast, memory-efficient multidimensional arrays (ndarray).
20
2.2 Example
import numpy a s np
4
a r r = np . a r r a y ( [ [ 1 , 2 ] , [ 3 , 4 ] ] )
p r i n t ( ” Array : \ n ” , a r r )
p r i n t ( ” Mean : ” , np . mean ( a r r ) )
p r i n t ( ” Transpose : \ n ” , a r r . T)
What is Pandas?
S
Provides high-level data structures like Series and DataFrame
.R
Built on top of NumPy
Dr
Ideal for data manipulation, cleaning, and analysis 5/
3.1 Key Data Structures
Structure
2
Description
20
Task
Grouping groupby ( )
5
3.3 Example
import pandas a s pd
d f = pd . r e a d c s v ( ’ data . csv ’ )
p r i n t ( d f . head ( ) )
S
4 Matplotlib – Visualization Library
.R
What is Matplotlib?
Dr
A plotting library for creating static, animated, and interactive visualizations in Python.
import m a t p l o t l i b . p y p l o t a s p l t
x = [1 , 2 , 3 , 4]
y = [ 1 0 , 15 , 13 , 17]
plt . plot (x , y)
6
p l t . t i t l e ( ” Simple Li ne P l o t ” )
p l t . x l a b e l ( ”X= a x i s ” )
p l t . y l a b e l ( ”Y= a x i s ” )
plt . grid ()
p l t . show ( )
S
Feature NumPy Pandas Matplotlib
Purpose Numerical computing Data manipulation Data visualization
.R
Main Structure Ndarray Series, DataFrame Figure, Axes
Strength Fast array math, broadcasting Intuitive data wrangling Visual storytelling with plots
Dr
Integration Foundation for Pandas & Scipy Built on NumPy Plots Pandas and NumPy outputs
What is NumPy?
Au
NumPy (Numerical Python) is a fundamental library for numerical and scientific computing in
Python. It provides:
7
5.1 Installing NumPy (if not already installed)
p i p i n s t a l l numpy
! p i p i n s t a l l numpy
S
5.2 Importing the NumPy Module
.R
import numpy a s np
Dr
np is the standard alias (short form) used by convention in the data science community. It allows
import numpy a s np
,
# C r e a t e a NumPy a r r a y
10
a r r = np . a r r a y ( [ 1 , 2 , 3 , 4 , 5 ] )
p r i n t ( ” Array : ” , a r r )
st
gu
# C a l c u l a t e mean
p r i n t ( ” Mean : ” , np . mean ( a r r ) )
Au
import numpy a s np
p r i n t ( np . version )
8
5.5 Summary
Code
Task
S
Data manipulation is the process of loading, cleaning, transforming, and organizing data
.R
into a useful format for analysis and visualization. Pandas is the go-to Python library for data
Intuitive syntax
Dr
5/
Powerful functions
2
Built-in support for real-world data formats (CSV, Excel, JSON, etc.)
20
Benefit
10
Feature
Works like Excel tables but more powerful
Easy handling of tabular data
For filtering, sorting, grouping, aggregating
High-level functions
st
9
6.3 Common Data Manipulation Tasks in Pandas
Pandas Function/Method
Task
S
Sort data d f . s o r t v a l u e s ( by=’ Score ’ , a s c e n d i n g=F a l s e )
.R
Group and summarize d f . groupby ( ’ Category ’ ) . mean ( )
# Load d a t a s e t
d f = pd . r e a d c s v ( ” s t u d e n t s . c s v ” )
,
10
p r i n t ( d f . head ( ) )
gu
# F i l t e r s t u d e n t s o l d e r than 18
Au
f i l t e r e d = d f [ d f [ ’ Age ’ ] > 1 8 ]
# Group by c l a s s and f i n d a v e r a g e s c o r e
10
a v g s c o r e s = d f . groupby ( ’ C l a s s ’ ) [ ’ Score ’ ] . mean ( )
6.5 Summary
S
Slice, filter, and reshape your data
.R
Generate insights through grouping, merging, and transformations
Dr
“If data is the new oil, Pandas is the refinery.” 5/
7 What is a Pandas Series?
2
A Series is a one-dimensional labeled array capable of holding any data type — integers, floats,
20
strings, Python objects, etc. Think of it as a column in a table or a 1D NumPy array with labels
(index).
,
10
import pandas a s pd
11
# From a l i s t
s 1 = pd . S e r i e s ( [ 1 0 , 2 0 , 3 0 , 4 0 ] )
p r i n t ( s1 )
Output:
0 10
1 20
2 30
S
3 40
dtype : i n t 6 4
.R
7.3 Custom Index in Series
Dr
s 2 = pd . S e r i e s ( [ 9 0 , 8 5 , 8 8 ] , i n d e x =[ ’ Math ’ , ’ S c i e n c e ’ , ’ E n g l i s h ’ ] )
5/
p r i n t ( s2 )
2
Math 90
20
Science 85
English 88
,
10
dtype : i n t 6 4
st
p r i n t ( s 2 [ ’ Math ’ ] ) # A c c e s s by l a b e l
p r i n t ( s2 [ 1 ] ) # A c c e s s by p o s i t i o n
Au
s 3 = pd . S e r i e s ( marks )
p r i n t ( s3 )
12
7.6 Useful Series Methods
Method
Description
s . mean ( ) Returns mean of the values
s . max ( ) / s . min ( ) Max or min value
s . head ( n ) First n elements
s . t a i l (n) Last n elements
s . sort values () Sorts values
S
s . value counts () Frequency count of unique values
s . apply ( f u n c ) Applies a function element-wise
.R
7.7 Example with Operations
s = pd . S e r i e s ( [ 1 , 2 , 3 , 4 ] )
Dr
5/
# Add 5 t o each v a l u e
2
20
p r i n t ( s + 5)
# Square each v a l u e
,
10
p r i n t ( s . apply ( lambda x : x * * 2 ) )
st
7.8 Summary
gu
Aspect Description
“Series is the building block of Pandas data structures — mastering it is the first
13
8 What is a Pandas DataFrame?
A DataFrame is a two-dimensional, labeled tabular data structure in Pandas. It’s similar to:
A SQL table,
S
Each column is a Series, and all columns share the same row index.
.R
8.1 Key Features of DataFrame
Feature Description
Dr
2D structure Rows and columns, like a table
Labeled axes Custom row and column names
5/
Heterogeneous types Different data types per column (int, float, string, etc.)
Integrated methods Easy filtering, reshaping, joining, grouping, etc.
2
20
import pandas a s pd
st
data = {
gu
’Name ’ : [ ’ A l i c e ’ , ’ Bob ’ , ’ C h a r l i e ’ ] ,
Au
’ Age ’ : [ 2 5 , 3 0 , 2 8 ] ,
d f = pd . DataFrame ( data )
print ( df )
14
8.2.2 From a List of Lists
data = [ [ ’ A l i c e ’ , 2 5 ] , [ ’ Bob ’ , 3 0 ] ]
d f = pd . r e a d c s v ( ” data . c s v ” )
S
8.2.4 Accessing Data in DataFrame
.R
Code Example
Task
Dr
Select multiple columns d f [ [ ’ Name ’ , ’ Age ’ ] ]
# Update a v a l u e
gu
d f . l o c [ 1 , ’ Age ’ ] = 31
Au
# Drop a column
15
8.2.6 Useful DataFrame Methods
Method
Description
d f . head ( n ) First n rows
df . t a i l (n) Last n rows
d f . shape (Rows, Columns)
d f . columns List of column names
df . i n f o () Summary of the DataFrame
S
df . describe () Summary statistics (for numeric columns)
df . i s n u l l () Check for missing values
.R
d f . dropna ( ) Remove rows with nulls
df . f i l l n a ( value ) Fill nulls with a value
8.2.9 Summary
Au
Aspect Value
Core structure 2D labeled data
Columns Represent different variables
Rows Represent observations/records
Strength Extremely flexible & powerful
16
9 Loading and Handling Data with Pandas
In data science, data often comes from external sources like CSV, Excel, SQL databases, JSON
S
.R
9.1 Loading Data Using Pandas
Function Example
Source Type
Dr
CSV File pd . r e a d c s v ( ) d f = pd . r e a d c s v ( ” data . c s v ” )
Clipboard pd . r e a d c l i p b o a r d ( ) d f = pd . r e a d c l i p b o a r d ( )
Command
Purpose
d f . head ( n ) First n rows (default 5)
st
17
[Link] Detecting Missing Data
S
df . f i l l n a (0) # F i l l m i s s i n g v a l u e s with 0
d f . f i l l n a ( method=’ f f i l l ’ ) # Forward f i l l m i s s i n g v a l u e s
.R
9.1.3 Data Type Conversion
d f [ ’ Age ’ ] = d f [ ’ Age ’ ] . a s t y p e ( i n t )
Dr
5/
d f [ ’ Date ’ ] = pd . t o d a t e t i m e ( d f [ ’ Date ’ ] )
2
9.1.4 Filtering and Subsetting Data
20
d f f i l t e r e d = d f [ d f [ ’ Age ’ ] > 2 5 ]
10
# S e l e c t s p e c i f i c columns
st
d f s u b s e t = d f [ [ ’ Name ’ , ’ Age ’ ] ]
gu
18
9.1.7 Saving Data to Disk
Command
Format
Excel d f . t o e x c e l ( ’ output . x l s x ’ )
JSON d f . t o j s o n ( ’ output . j s o n ’ )
S
import pandas a s pd
.R
# Load data
Dr
d f = pd . r e a d c s v ( ” s t u d e n t s . c s v ” )
5/
# Clean data
d f . dropna ( i n p l a c e=True )
2
d f [ ’ Score ’ ] = d f [ ’ Score ’ ] . a s t y p e ( i n t )
20
# F i l t e r and s o r t
,
10
# Save r e s u l t s
st
h i g h s c o r e r s . t o c s v ( ” t o p s t u d e n t s . c s v ” , i n d e x=F a l s e )
gu
Au
19
9.1.9 Summary
Function
Step
S
“Pandas makes data loading and cleaning seamless, turning messy data into pow-
.R
erful insights.”
Dr
10 Introduction to Matplotlib
It is the foundation for other libraries like Seaborn, Pandas plotting, and Plotly (to some extent).
,
10
import m a t p l o t l i b . p y p l o t a s p l t
st
gu
Compatible with NumPy Works directly with arrays and Pandas objects
20
import m a t p l o t l i b . p y p l o t a s p l t
x = [1 , 2 , 3 , 4]
y = [ 1 0 , 20 , 25 , 30]
plt . plot (x , y)
p l t . t i t l e ( ” B a s i c L in e P l o t ” )
p l t . x l a b e l ( ”X= a x i s ” )
S
p l t . y l a b e l ( ”Y= a x i s ” )
.R
p l t . g r i d ( True )
p l t . show ( )
p l t . t i t l e ( ” Customized P l o t ” )
p l t . x l a b e l ( ” Time ” )
p l t . y l a b e l ( ” Value ” )
p l t . l e g e n d ( [ ” Lin e A” ] )
21
p l t . g r i d ( True )
10.5 Subplots
p l t . subplot (1 , 2 , 1) # 1 row , 2 c o l s , 1 s t p l o t
plt . plot (x , y)
S
p l t . subplot (1 , 2 , 2) # 2nd p l o t
.R
p l t . bar ( x , y )
p l t . show ( )
import m a t p l o t l i b . p y p l o t a s p l t
st
x = [1 , 2 , 3 , 4 , 5]
gu
y1 = [ 1 0 , 2 0 , 2 5 , 3 0 , 4 0 ]
y2 = [ 5 , 1 5 , 2 0 , 2 5 , 3 0 ]
Au
p l t . p l o t ( x , y1 , l a b e l =’ S a l e s ’ )
p l t . p l o t ( x , y2 , l a b e l =’ P r o f i t ’ )
p l t . t i t l e ( ” S a l e s vs P r o f i t ” )
p l t . x l a b e l ( ” Quarter ” )
p l t . y l a b e l ( ” Amount ” )
22
plt . legend ()
p l t . show ( )
S
plt . scatter () Scatter plot
plt . t i t l e () Set title
.R
plt . xlabel () X-axis label
plt . ylabel () Y-axis label
Dr
plt . legend () Add legend
plt . grid () Show grid
5/
plt . savefig () Save figure to file
2
20
Matplotlib is a versatile and widely-used Python library for creating static, animated, and inter-
10
Data exploration
st
import m a t p l o t l i b . p y p l o t a s p l t
23
import m a t p l o t l i b . p y p l o t a s p l t
x = [1 , 2 , 3 , 4 , 5]
y = [ 1 0 , 12 , 8 , 14 , 7 ]
plt . plot (x , y)
p l t . t i t l e ( ” B a s i c L in e P l o t ” )
p l t . x l a b e l ( ”X= a x i s ” )
S
p l t . y l a b e l ( ”Y= a x i s ” )
.R
p l t . g r i d ( True )
p l t . show ( )
p l t . t i t l e ( ” S a l e s Over Time ” )
p l t . x l a b e l ( ”Week” )
p l t . y l a b e l (” S a l e s ”)
st
p l t . g r i d ( True )
gu
p l t . show ( )
Au
p r o d u c t s = [ ’ A’ , ’B’ , ’C ’ ]
s a l e s = [ 1 0 0 , 150 , 90]
p l t . bar ( p r o d u c t s , s a l e s , c o l o r =’ green ’ )
24
p l t . t i t l e ( ” Product S a l e s ” )
p l t . x l a b e l ( ” Product ” )
p l t . y l a b e l (” S a l e s ”)
p l t . show ( )
l a b e l s = [ ’ Apples ’ , ’ Bananas ’ , ’ C h e r r i e s ’ ]
S
s i z e s = [ 3 0 , 45 , 25]
.R
p l t . p i e ( s i z e s , l a b e l s=l a b e l s , a u t o p c t = ’%1.1 f %%’, s t a r t a n g l e =90)
p l t . t i t l e (” Fruit S a l e s D i s t r i b u t i o n ”)
p l t . show ( )
Dr
5/
11.5 Scatter Plot Example
2
20
x = [ 1 0 , 20 , 30 , 40]
y = [ 5 , 15 , 10 , 20]
,
10
p l t . s c a t t e r ( x , y , c o l o r =’ red ’ )
p l t . x l a b e l ( ” Study Hours ” )
gu
p l t . y l a b e l ( ” Marks ” )
p l t . show ( )
Au
import numpy a s np
25
p l t . h i s t ( data , b i n s =30 , c o l o r =’ p u r p l e ’ )
p l t . t i t l e ( ” D i s t r i b u t i o n o f Values ” )
p l t . x l a b e l ( ” Value ” )
p l t . y l a b e l ( ” Frequency ” )
p l t . show ( )
S
data = [ 7 , 8 , 5 , 6 , 1 0 , 9 , 6 , 4 , 9 , 1 1 ]
.R
p l t . b o x p l o t ( data )
p l t . t i t l e ( ” Boxplot o f S c o r e s ” )
p l t . show ( )
Dr
5/
11.8 Saving the Plot
2
20
p l t . subplot (1 , 2 , 1)
st
p l t . t i t l e ( ” L in e ” )
Au
p l t . subplot (1 , 2 , 2)
p l t . bar ( [ ’ A’ , ’B ’ ] , [ 3 , 5 ] )
p l t . t i t l e ( ” Bar ” )
p l t . show ( )
26
11.10 Summary Table
Command/Function
Task
S
Histogram plt . hist ()
.R
Show plot p l t . show ( )
Matplotlib supports a wide variety of charts and graphs useful for data visualization, pattern
Goal: Understand the syntax, purpose, and example use of each chart type.
import m a t p l o t l i b . p y p l o t a s p l t
Au
x = [1 , 2 , 3 , 4 , 5]
y = [2 , 4 , 1 , 8 , 7]
p l t . t i t l e ( ” S c a t t e r P l o t Example ” )
27
p l t . x l a b e l ( ”X Values ” )
p l t . y l a b e l ( ”Y Values ” )
p l t . g r i d ( True )
p l t . show ( )
S
c a t e g o r i e s = [ ’ A’ , ’B’ , ’C ’ ]
.R
values = [ 2 0 , 35 , 30]
p l t . bar ( c a t e g o r i e s , v a l u e s , c o l o r =’ blue ’ )
p l t . x l a b e l ( ” Category ” ) Dr
5/
p l t . y l a b e l ( ” Values ” )
2
p l t . show ( )
20
l a b e l s = [ ’ Math ’ , ’ S c i e n c e ’ , ’ E n g l i s h ’ ]
st
s i z e s = [ 3 0 , 40 , 30]
gu
p l t . t i t l e ( ” Pi e Chart Example ” )
p l t . a x i s ( ’ eq ual ’ ) # Equal a s p e c t r a t i o f o r a c i r c l e
p l t . show ( )
28
x = [1 , 2 , 3 , 4 , 5]
y = [ 1 0 , 12 , 15 , 13 , 18]
p l t . t i t l e ( ” L in e Chart Example ” )
p l t . x l a b e l ( ” Time ” )
p l t . y l a b e l (” S a l e s ”)
p l t . g r i d ( True )
S
p l t . show ( )
.R
12.5 Histogram
Dr
To display distribution of numerical data using bins.
import numpy a s np
2 5/
data = np . random . randn ( 1 0 0 0 )
20
p l t . t i t l e ( ” Histogram Example ” )
10
p l t . x l a b e l ( ” Value ” )
p l t . y l a b e l ( ” Frequency ” )
st
p l t . g r i d ( True )
gu
p l t . show ( )
Au
data = [ 7 , 8 , 6 , 9 , 1 0 , 1 3 , 6 , 5 , 9 , 1 1 ]
p l t . b o x p l o t ( data )
29
p l t . t i t l e ( ” Box P l o t Example ” )
p l t . y l a b e l (” Scores ”)
p l t . show ( )
x = [1 , 2 , 3 , 4 , 5]
S
y1 = [ 1 , 4 , 6 , 8 , 9 ]
.R
y2 = [ 2 , 2 , 7 , 1 0 , 1 2 ]
p l t . s t a c k p l o t ( x , y1 , y2 , l a b e l s =[ ’A’ , ’B ’ ] , c o l o r s =[ ’ s k y b l u e ’ , ’ orange ’ ] )
p l t . t i t l e ( ” Area P l o t Example ” )
p l t . l e g e n d ( l o c =’ upper l e f t ’ ) Dr
5/
p l t . show ( )
2
20
Same as bar chart, but horizontally for better readability of long labels.
,
10
popularity = [ 4 5 , 30 , 20]
st
p l t . barh ( l a n g s , p o p u l a r i t y , c o l o r =’ t e a l ’ )
gu
p l t . t i t l e ( ” H o r i z o n t a l Bar Chart ” )
p l t . x l a b e l ( ” P o p u l a r i t y (%)”)
Au
p l t . show ( )
30
12.9 Comparison Table of Chart Types
Function
Chart Type Use Case
S
Histogram Frequency distribution plt . hist ()
pl t . boxplot ()
.R
Box Plot Summary statistics + outliers
Dr
2 5/
, 20
10
st
gu
Au
31
Au
gu
st
10
, 20
2 5/
Dr
.R
S
Au
gu
st
10
, 20
2 5/
Dr
.R
S
Au
gu
st
10
, 20
2 5/
Dr
.R
S
Au
gu
st
10
, 20
2 5/
Dr
.R
S
References
23.07.2025
S
[5] URL [Link] Access 23.07.2025
.R
[6] URL [Link] Access 28.03.2025
Dr
********************
2 5/
, 20
10
st
gu
Au
36