0% found this document useful (0 votes)
2 views171 pages

Python Unit4

The document provides an overview of NumPy and Pandas, two essential libraries in Python for numerical computing and data analysis. It highlights the advantages of using NumPy, such as performance and memory efficiency, and describes the functionalities of Pandas for handling structured data. Additionally, it covers data visualization techniques using libraries like Matplotlib, emphasizing the importance of visual representation in data analysis.

Uploaded by

rudravsharma2580
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views171 pages

Python Unit4

The document provides an overview of NumPy and Pandas, two essential libraries in Python for numerical computing and data analysis. It highlights the advantages of using NumPy, such as performance and memory efficiency, and describes the functionalities of Pandas for handling structured data. Additionally, it covers data visualization techniques using libraries like Matplotlib, emphasizing the importance of visual representation in data analysis.

Uploaded by

rudravsharma2580
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON

UNIT-4

ANSHU SHARMA
• NumPy (Numerical Python) is a fundamental library in Python for
numerical computing, providing support for large, multi-dimensional
arrays and matrices, along with a collection of mathematical functions
to operate on these arrays efficiently. It is widely used for scientific
computing, data analysis, and machine learning due to its speed and
versatility.

ANSHU SHARMA
Why Use NumPy in Python?
• Performance – NumPy arrays are faster than Python lists due to optimized
C-based implementation.
• Efficient Memory Usage – NumPy uses contiguous memory storage,
making data access and operations more efficient.
• Multi-Dimensional Support – It allows handling multi-dimensional arrays
and matrices seamlessly.
• Vectorized Operations – Eliminates the need for explicit loops, making
computations faster.
• Broadcasting – Enables element-wise operations without creating
unnecessary copies of data.
• Extensive Mathematical Functions – Supports a wide range of mathematical
operations, including linear algebra, statistical functions, and Fourier
transforms.
• Integration with Other Libraries – Works well with Pandas, SciPy,
TensorFlow, and other data science and machine learning libraries.
ANSHU SHARMA
NumPy
• NumPy is a Python Package which stands for “Numerical Python”
• This was created in 2005 by Travis Oliphant
• Using NumPy we can perform the following functionality:
1. Mathematical and logical calculation on Array
2. Fourier transformation and routine for shape manipulation
3. Used for scientific calculation
4. It is faster than Python List
5. Fasy because it is associated with C Programming

ANSHU SHARMA
• NumPy is often called as an alternate for MatLab
• NumPy is used with package like SciPy(Scientific Python)
1. Dimensional Array is referred to as Vector
2. Dimensional Array is referred to as Matrix
3. Dimensional Array is referred to as Tensor

ANSHU SHARMA
• NumPy is the fundamental package for scientific computing in Python
• NumPy is a Python library that provides a multidimensional array
object, various derived object.
• pip install NumPy for installing NumPy
• In NumPy array is a fundamental object
• nd array created here
• Used to store homogeneous data element in a contiguous block of
element

ANSHU SHARMA
Array VS List
• All elements of array are of same • List can have element of
data type different data type e.g
• Elements of array store in [1,2.3,’hello’]
contiguous memory location • Not stored in contiguous
memory location
• Array are static and cannot be • List can be resized and modified
resized once they are created easily
• NumPy array takes up less space • More space in memory
in memory

ANSHU SHARMA
• Advantage of using NumPy Array over Python List:
Consume less memory
Fast as compared to the python list
Convenient to use

ANSHU SHARMA
Creating 1-D Array in NumPy
• cmd →pip import numpy
• IDLE prompt → import numpy as np (np is alias i.e dummy)

ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
2D-Array in NumPy

ANSHU SHARMA
ANSHU SHARMA
Attributes of NumPy Arrays
1. ndim #Dimension – No of dimensions of an array
2. shape -- size of array in each dimension
3. Size – total no of elements in array
4. Dtype – datatype of element in array
5. Itemsize – size of each element (in bytes)

ANSHU SHARMA
ANSHU SHARMA
Indexing

ANSHU SHARMA
ANSHU SHARMA
Slicing

ANSHU SHARMA
ANSHU SHARMA
Arithmetic Operations
1. Addition
2. Subtraction
3. Multiplication
4. Matrix multiplication
5. Division
6. Floor division
7. Exponential
8. Modulo
9. Transpose

ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
Padas
• Pandas stands for “Python Data Analysis Library.”
• Pandas is a Python library.
• Pandas is used to analyze data.
• Pandas allows us to analyze big data and make conclusions based on
statistical theories.
• Pandas can clean messy data sets, and make them readable and
relevant.
• It is an open-source library built on top of NumPy, and it is mainly
used for data manipulation and data analysis.

ANSHU SHARMA
NumPy Pandas

• For working with large • For working with


numerical arrays or matrices. structured/tabular data (like
excel, CSV).
• If we need high performance • If we need powerful data
mathematical operations. manipulation tools (grouping,
filtering, handelling missing
data)
• Better for numerical and • Better for data analysis and
scientific computation manipulation

ANSHU SHARMA
• Pandas store tabular data using a DataFrames.
• A dataframe is a two-dimensional labelled data structure like a table in
databases
• Every dataframe contains rows and column, and therefor has both a
row and column index
• Each column can have a different type of values
• You can read, clean, filter, and group data with just a few lines.
• It’s one of the most important tools for data analysis in Python.

ANSHU SHARMA
3 Data Structure
• 1. Series - 1D
• 2. DataFrame – 2D
• 3. Panel – 3D

ANSHU SHARMA
Series DataFrame

• A one-dimensional labelled • A two-dimensional labelled array


array. • Can hold multiple data type
• It can hold any data type across column
(integer, string, float) • Like a table or spreadsheet with
• Like a single column of table rows and column
• Ideal for storing and working • Suitable for datasets with
with a single column data multiple column

ANSHU SHARMA
• Series → Data represented in I D array
We take list as argument
• DataFrame → Data represent in 2 D array
We pass list/Dictionary/ series as other data frame
• Panel → To represent in a Multidimensional array
We pass the data, major axis, minor axis

ANSHU SHARMA
Series
A Series in Pandas is a one-dimensional labeled array capable of
holding data of any type, including integers, floats, strings, and Python
objects. It consists of two main components −
Data: The actual values stored in the Series.
Index: The labels or indices that correspond to each data value.

ANSHU SHARMA
Creating a Pandas Series
class [Link](data, index, dtype, name, copy)

#Create an Empty Series


import pandas as pd
s = [Link]()
# Display the result
print('Resultant Empty Series:\n’,s)

##OUTPUT: Resultant Empty Series:


Series([], dtype: object)

ANSHU SHARMA
ANSHU SHARMA
Common Operations
Mean()
sum()
max()
min()
s.[1:3] # Slicing like in Python lists

ANSHU SHARMA
mean() – Average (Mean)
• This returns the average of all numerical values in the Series.
Example:
import pandas as pd
s = [Link]([10, 20, 30, 40, 50])
print("Mean:", [Link]())
Output: Mean: 30.0

ANSHU SHARMA
sum() – Sum (Total)
Returns the sum of all values in the Series.
Example:
s = [Link]([10, 20, 30, 40, 50])
print("Sum:", [Link]())
Output: Sum: 150

ANSHU SHARMA
max() – Maximum Value
Finds and returns the largest value in the Series.
Example:
s = [Link]([10, 20, 30, 40, 50])
print("Max:", [Link]())
Output: Max: 50

ANSHU SHARMA
• s[1:3] – Slicing (like Python lists)
• Returns a subset of the Series from index 1 to 2 (end is exclusive).
Example:
• print("Sliced Series (s[1:3]):\n", s[1:3])
• Output:Sliced Series (s[1:3]):
1 20
2 30
dtype: int64

ANSHU SHARMA
Installation
• cmd → pip install pandas
• IDLE → import pandas as pd

ANSHU SHARMA
Manually

ANSHU SHARMA
By CSV files

ANSHU SHARMA
Operations
• head()
• tail()
• shape()
• column()
• size()
• dtype()
• value()
• index()

ANSHU SHARMA
• head() → By default, show top 5 rows
• If we provide value, then it will show as much value as we have
provided

ANSHU SHARMA
• tail()→ show last 5 rows by default

ANSHU SHARMA
• shape() → Returns a tuple representing the dimensionality of the
DataFrame as (rows,column)
• Know how many rows and column your data has

ANSHU SHARMA
columns → Return the column labels of the DataFrames
Get all the column names in your DataFrame

ANSHU SHARMA
• Size → Return the no of elements in the DataFrame (rows * column)
• Total no of cells in the DataFrame

ANSHU SHARMA
• Dtypes → return the datatype of each column

ANSHU SHARMA
• values →Return the data of thr DataFrame
• Extract raw data in array format

ANSHU SHARMA
• Index → Returns the index of the DataFrame
• Check how rows are labelled or numbered

ANSHU SHARMA
Data Visualization
• Data visualization in Python involves using libraries to create
graphical representations of data, making it easier to understand
patterns, trends, and outliers.
• Data visualization is a powerful tool for exploring and communicating
insights from data. Python's versatile libraries make it a popular choice
for data analysis and visualization tasks.

ANSHU SHARMA
Python provides various libraries that come with different features for
visualizing data. All these libraries come with different features and can
support various types of graphs.
Key Libraries:
• Matplotlib
• Seaborn
• Plotly
• Pandas
• Altair
• Bokeh
• Geoplotlib
• GGplot
• Pygal

ANSHU SHARMA
Common Plot Types:
• Line charts: Show trends over time or continuous data.
• Bar charts: Compare discrete categories or groups.
• Histograms: Display the distribution of numerical data.
• Scatter plots: Show the relationship between two numerical variables.
• Box plots: Display the distribution of numerical data and identify
outliers.
• Heatmaps: Show the correlation between variables.
• Maps: Display geographical data.

ANSHU SHARMA
Benefits of Data Visualization:
• Improved understanding: Visuals make complex data easier to grasp.
• Pattern identification: Trends and relationships become more apparent.
• Outlier detection: Unusual data points are easily spotted.
• Effective communication: Data insights can be shared clearly with
stakeholders.
• Decision support: Visualizations aid in making data-driven decisions.

ANSHU SHARMA
Matplotlib
• Matplotlib is a powerful and versatile open-source plotting library for
Python, designed to help users visualize data in a variety of formats.
Developed by John D. Hunter in 2003, it enables users to graphically
represent data, facilitating easier analysis and understanding. If you
want to convert your boring data into interactive plots and
graphs, Matplotlib is the tool for you.

ANSHU SHARMA
• Linear Plot
• Scatter plot
• Bar plot
• Stem plot
• Step plot
• Hist plot
• Box plot
• Pie plot
• Fill_Between plot

ANSHU SHARMA
Linear Plot

ANSHU SHARMA
Scatter Plot

ANSHU SHARMA
Bar Plot

ANSHU SHARMA
Stem Plot

ANSHU SHARMA
Step Plot

ANSHU SHARMA
Histogram Plot

ANSHU SHARMA
Box Plot

ANSHU SHARMA
Pie plot

ANSHU SHARMA
Fill_Between Plot

ANSHU SHARMA
Installation of matplotlib
pip install matplotlib

ANSHU SHARMA
Importing matplotlib
import [Link] as plt

Or

from matplotlib import pyplot as plt

ANSHU SHARMA
ANSHU SHARMA
Bar Plot

ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
Align has 2 parameters:- edge, center
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
linestyle='--': Dashed border ('--', ':', '-.', etc.
ANSHU SHARMA
ANSHU SHARMA
Alpha value varies between 0 to 1
ANSHU SHARMA
To work on Label we have to add legend
ANSHU SHARMA
Scatter Plot
• A scatter plot in Python shows points scattered in a graph — usually
used to see relationship between two [Link] can create a simple
scatter plot using [Link]().

ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
Matplotlib. Valid color codes include
• 'r' (red),
• 'g' (green),
• 'b' (blue),
• 'y' (yellow),
• 'c' (cyan),
• 'm' (magenta),
• 'k' (black),
• 'w' (white),

ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
Common marker styles:
Marker Description
'o' Circle
'^' Triangle up
'v' Triangle down
's' Square
'p' Pentagon
'*' Star
'+' Plus
'x' X
'D' Diamond
'h' Hexagon
'.' Point (very small)

ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
Common Colormaps:
Name Description
'viridis' Default, perceptually uniform
'plasma' Bright and colorful
'inferno' Dark, good for grayscale printing
'magma' Warm and dark
'cividis' Colorblind-friendly
'cool' Cyan to magenta
'hot' Black–red–yellow–white
'jet' Rainbow-like (less recommended)
'rainbow' Classic rainbow

Notes:
Use c= to pass the color data.
Use cmap= to choose the color map.
Use [Link]() to add a legend showing the color scale.
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
Histogram

ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
Bins parameter
• Number of bins (default is 10) or a sequence to define custom bin
edges.
• [Link](data, bins=5)
• → Divides the data into 5 equal intervals.
• bins = [0, 10, 20, 40, 60, 100]
• [Link](data, bins=bins)

ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
Align can be: left, right,mid
ANSHU SHARMA
histtype: Type of histogram
('bar', 'barstacked', 'step',
'stepfilled').
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
Box Plot

ANSHU SHARMA
A box plot (also called a box-and-whisker plot) is a graphical
representation of the distribution, spread, and skewness of a dataset.
It summarizes data using five-number statistics:
[Link]
[Link] Quartile (Q1)
[Link] (Q2)
[Link] Quartile (Q3)
[Link]

ANSHU SHARMA
• Common Parameters in [Link]():
• vert=True: Vertical box plot (set to False for horizontal).
• patch_artist=True: Fills boxes with color.
• notch=True: Adds a notch around the median.
• labels=[...]: Labels for each dataset.

ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
Default width=0.2
ANSHU SHARMA
ANSHU SHARMA
Seaborn
• Seaborn is a powerful Python data visualization library based on
matplotlib, built specifically for creating attractive and informative
statistical graphics. It provides a high-level interface for drawing
attractive and complex plots with less code.

• Cmd → pip install seaborn


• Idle → import seaborn as sns

ANSHU SHARMA
Seaborn graphs types
• Scatter Plot
• Box plot
• Violin plot
• Swarn plot
• Heatmap
• Histogram
• Bar plot
• Factor plot
• Density plot

ANSHU SHARMA
[Link]
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
BAR CHART
• A bar chart in Seaborn is a type of plot used to display the
relationship between a categorical variable and a numerical variable
by showing rectangular bars. Each bar’s height (or length) represents
a summary statistic (like mean, sum, or count) of the numeric
variable for each category.

ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
SCATTER PLOT

ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
Pair Plot

ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
HEATMAP
• A heatmap in Seaborn is a data visualization technique that displays
matrix-like data (such as a correlation matrix or pivot table) using
color gradients to indicate values. It's commonly used to visualize
relationships, patterns, and intensities in two-dimensional data.

ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
ANSHU SHARMA
KINDLY COMPLETE HEATMAP AND DISTRIBUTION PLOT YOURSELF

ANSHU SHARMA

You might also like