0% found this document useful (0 votes)
4 views25 pages

Module 5 Python Notes PDF

Module 5 covers Python's Standard Library, emphasizing the use of modules and packages for code organization and reusability. It introduces NumPy for numerical computing and Pandas for data manipulation, detailing their functionalities and operations. The document provides examples of creating and using modules, as well as performing operations with NumPy arrays and Pandas DataFrames.
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)
4 views25 pages

Module 5 Python Notes PDF

Module 5 covers Python's Standard Library, emphasizing the use of modules and packages for code organization and reusability. It introduces NumPy for numerical computing and Pandas for data manipulation, detailing their functionalities and operations. The document provides examples of creating and using modules, as well as performing operations with NumPy arrays and Pandas DataFrames.
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

Module 5 – Python’s Standard Library

The Python Standard Library is a collection of built-in modules and functions that come pre-
installed with Python. It provides ready-made tools for tasks such as mathematical operations, file
handling, system interaction, and data processing. The standard library makes programming easier
and faster by allowing developers to reuse existing code instead of writing everything from scratch.

[Link] and packages

Python is a very powerful programming language because it provides ready-made functionality


through modules and packages. When programs become large, writing everything in a single file
becomes difficult to manage. To solve this problem, Python allows us to divide programs into
separate files called modules and folders called packages. This improves readability, reusability, and
maintainability of code.

What is a module ?
A module is a Python file containing functions, variables, and classes. The file must have a .py
extension. Modules allow us to organize related code into separate files.

Features of Modules:

.Promote code reusability


.Improve program structure
.Reduce code duplication
.Provide namespace separation

Create a Module
To create a Python module, write the desired code and save that in a file with .py extension.

Example: Let's create a [Link] in which we define two functions, one add and another subtract.

Python
# [Link]
def add(x, y):
return (x+y)

def subtract(x, y):


return (x-y)

Import Module

Modules can be used in another Python file using the import statement. When Python sees
an import, it loads the module if it exists in the interpreter’s search path.

syntax to import a module:


import module_name

Example: Here, we are importing the calc that we created earlier to perform add operation.

Python
import calc
print([Link](10, 2))

Output
12

Explanation: import calc loads the module and [Link]() accesses a function through dot
notation.

Types of Import Statements

1. Import From Module:

This allows importing specific functions, classes, or variables rather than the whole module.

Python
from math import sqrt, factorial
print(sqrt(16))
print(factorial(6))

Output
4.0
720

Explanation: Only sqrt and factorial are brought into the local namespace, so the prefix
math. is not required.

2. Import All Names:

* imports everything from a module into the current namespace.

Python
from math import *
print(sqrt(16))
print(factorial(6))

Output
4.0
720

Explanation: Every public name of math becomes directly accessible. (Not recommended in
large projects due to namespace conflicts.)
3. Import With Alias:

You can shorten a module’s name using as.

Python
import math as m
print([Link])

Output
3.141592653589793

Explanation: math is accessed through the shorter alias m.

Types of Modules

Python provides several kinds of modules. Each type plays a different role in application
development.

1. Built-in Modules:

These come bundled with Python and require no installation - e.g., math, random, os.

Python
import random
print([Link](1, 5))

Output
4

Explanation: [Link]() returns a random number within the given range.

2. User-Defined Modules:

These are modules you create yourself, such as [Link].

Python
import calc
print([Link](20, 5))

Output

15

Explanation: The module is created manually and then imported into another script.

3. External (Third-Party) Modules:


These modules are installed using pip - e.g., NumPy, Pandas, Requests.

Python
import requests
r = [Link]("[Link]
print(r.status_code)

Output
200

Explanation: requests is installed separately (pip install requests) and provides HTTP
utilities.

4. Package Modules:
A package is a directory containing multiple modules, usually with an __init__.py file.

Example Directory

mypkg/
__init__.py
[Link]
[Link]

Using a module from a package

Python
from mypkg import utils
print(utils.some_func())

Explanation:
calls a function named some_func(), the output will be whatever that function returns.

If [Link] contains something like:

Python
def some_func():
return "Hello"

Output
Hello

Numpy

What is Numpy?
• NumPy stands for Numerical python
• NumPy is a python library used for working with array
• NumPy is a fundamental package for scientific computing in python.
• It is also has functions for working in domain of linear algebra, Fouriern transform,
and matrices
• NumPy was created in 2005 by travis [Link] is an open source project and you
can use it freely

Why use NumPy ?


• In Python we have lists that serve the purpose of arrays, but they are slow to process.
• NumPy aims to provide an array object that is up to 50x faster than traditional
Python lists.
• The Array object in NumPy is called ndarray, it provides a lot of supporting
functions that make working with ndarray very easy.
• Arrays are very frequently used in data science, where speed and resources are very
important.

Arrays in NumPy
• An Array is a collection of values, organized in a specific order.
• NumPy’s main object is the homogeneous multidimensional array.
• It is a table of elements (usually numbers), all of the same type, indexed by a tuple
of positive integers.
• In NumPy dimensions are called axis. The number of axis is rank.
• NumPy’s array class is called ndarray. It is also known by the alias array.
NumPy
Creating Numerical Arrays
import numpy as np

# 1D array
arr1 = [Link]([1, 2, 3, 4, 5])

#2D array
arr2 = [Link]([[1, 2, 3], [4, 5, 6]])

print(arr1)
print(type(arr1))
print(arr2)

1-D Array
An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array. These
are the most common and basic arrays

Import numpy as np
arr = [Link]([1, 2, 3, 4, 5])
print(arr)

2-D Array
An array that has 1-D arrays as its elements is called a 2-D array. These are often used to
represent matrix or 2nd order tensors.

Import numpy as np
arr = [Link]([[1, 2, 3], [4, 5, 6]])
print(arr)

3-D Arrays

An array that has 2-D arrays (matrices) as its elements is called 3-D array. These are often
used to represent a 3rd order tensor.

Import numpy as np
arr = [Link]([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(arr)
Access Array Elements – NumPy

• Array indexing is the same as accessing an array element.


• You can access an array element by referring to its index number.
• The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and
the second has index 1 etc.

Example:
import numpy as np
arr = [Link]([1, 2, 3, 4])
print(arr[0])

Access 2-D Arrays


• To access elements from 2-D arrays we can use comma separated integers representing the
dimension and the index of the element.
• Think of 2-D arrays like a table with rows and columns, where the dimension represents
the row and the index represents the column.

Example
import numpy as np
arr = [Link]([[1,2,3,4,5], [6,7,8,9,10]])
print('2nd element on 1st row: ', arr[0, 1])

Access 3-D Arrays


• To access elements from 3-D arrays we can use comma separated integers representing the
dimensions and the index of the element.

Example
import numpy as np
arr = [Link]([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr[0, 1, 2])

NumPy Operations
1. Addition

[Link](x, y) or x + y
Adds corresponding elements of two arrays.
Example:

import numpy as np
a = [Link]([1, 2, 3])
b = [Link]([4, 5, 6])
print([Link](a, b))

Output:
[5 7 9]

2. Subtraction

[Link](x, y) or x - y
Subtracts corresponding elements of two arrays.

Example:
print([Link](a, b))
Output:
[-3 -3 -3]

3. Division

[Link](x, y) or x / y
Divides corresponding elements of two arrays.

Example:
print([Link](a, b))

4. Multiplication

[Link](x, y) or x * y
Multiplies corresponding elements of two arrays.

Example:
print([Link](a, b))

5. Square Root

[Link](x)
Returns square root of each element.
Example:

print([Link](a))

6. Sine
[Link](x)
Returns sine of each element (in radians).

Example:
print([Link](a))

7. Cosine
[Link](x)
Returns cosine of each element (in radians).

Example:
print([Link](a))

8. Logarithm
[Link](x)
Returns natural logarithm (base e) of each element.

Example:
print([Link](a))

9. Dot Product

[Link](x, y)
Returns dot product of two arrays.

Example:
print([Link](a, b))

10. Roots of Polynomial


[Link]([1, 0, -4])
Finds roots of polynomial equation.
Example:

print([Link]([1, 0, -4]))

This represents equation:


x² - 4 = 0

Output:
[ 2. -2.]

Built-in Functions in NumPy


NumPy provides many built-in functions to perform mathematical and statistical operations
directly on arrays. These functions work element-wise and are faster than normal Python
loops.

1. [Link]()

Definition:
Returns the sum of all elements in the array.

Syntax:
[Link](array)

Example:
import numpy as np
arr = [Link]([1, 2, 3, 4])
print([Link](arr))

For 2-D array:


[Link](arr, axis=0) # column-wise sum
[Link](arr, axis=1) # row-wise sum

2. [Link]()

Definition:
Returns the average of array elements.

syntax
[Link](arr)
3. [Link]()

Definition:
Returns the middle value of elements.

Syntax:
[Link](arr)

4. [Link]()

Definition:
Returns the standard deviation of elements.

Syntax:
[Link](arr)

5. [Link]()

Definition:
Returns the variance of elements.

Syntax:
[Link](arr)

6. [Link]()

Definition:
Returns the smallest element in the array.
Syntax:
[Link](arr)

7. [Link]()

Definition:
Returns the largest element in the array.
Syntax:
[Link](arr)

8. [Link]()

Definition:
Returns the square root of each element.
Syntax:
[Link](arr)

9. [Link]()

Definition:
Raises each element to a specified power.

Syntax:
[Link](arr, 2)

10. [Link]()

Definition:
Returns absolute (positive) values.

Syntax:
[Link](arr)

11. [Link]()

Definition:
Rounds off decimal values.

Syntax:
[Link](arr)

12. [Link]()
Definition:
Returns sine of each element (in radians).

Syntax:
[Link](arr)

13. [Link]()
Definition:
Returns cosine of each element (in radians).

Syntax:
[Link](arr)

14. [Link]()
Definition:
Returns natural logarithm (base e).

Syntax:
[Link](arr)

15. [Link]()

Definition:
Sorts the elements of an array.

Syntax:
[Link](arr)

16. [Link]()
Definition:
Changes the shape of the array without changing data.

[Link](arr, (rows, columns))


17. [Link]()
Definition:
Interchanges rows and columns.

Syntax:
[Link](arr)

18. [Link]()
Definition:
Joins two arrays together.

Syntax:
[Link]((a, b))

19. [Link]()
Definition:
Returns dot product of two arrays.

Syntax:
[Link](a, b)
Pandas
• Pandas is a Python library used for working with data sets.
• It has functions for analyzing, cleaning, exploring, and manipulating data.
• The name "Pandas" has a reference to both "Panel Data", and "Python Data Analysis" and
was created by Wes McKinney in 2008.

Usage:

• 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.
• Relevant data is very important in data science.

Installation of Pandas

If you have Python and PIP already installed on a system, then installation of Pandas is very
easy.
Install it using this command:

C:\Users\Your Name> pip install pandas

Import Pandas
Once Pandas is installed, import it in your applications by adding the import keyword:

import pandas

Core Components of Pandas: Series & DataFrames


The primary two components of pandas are the Series and DataFrame.
• Series is essentially a column, and
• DataFrame is a multi-dimensional table made up of a collection of Series.

DataFrames and Series are quite similar in that many operations that you can do with one
you can do with the other, such as filling in null values and calculating the mean.

• A DataFrame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion


in rows and columns.

Features of DataFrame:
• Potentially columns are of different types
• Size – Mutable
• Labeled axes (rows and columns)
• Can perform Arithmetic operations on rows and columns
Pandas Series

• A Pandas Series is like a column in a table.


• It is a one-dimensional array holding data of any type

Example:

import pandas as pd
a=[1, 7, 2]
arr=[Link](a)
print(arr)

Labels
If nothing else is specified, the values are labeled with their index number. First value has
index 0, second value has index 1 etc.

This label can be used to access a specified value.

print(myvar[0]) # output?

Create Labels

With the index argument, you can name your own labels.

Example:

import pandas as pd
a = [1, 7, 2]
arr = [Link](a, index = ["x", "y", "z"])
print(arr)
print(arr["y"])

Key/Value Objects as Series:


You can also use a key/value object, like a dictionary, when creating a Series.

Example:
import pandas as pd
calories = {"day1": 420, "day2": 380, "day3": 390}
myvar = [Link](calories)
print(myvar)
var1 = [Link](calories, index = ["day1", "day2"])
print(var1)
DataFrames
Data sets in Pandas are usually multi-dimensional tables, called DataFrames.
Series is like a column, a DataFrame is the whole table.

Example:

import pandas as pd

data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}

myvar = [Link](data, index = ["day1", "day2", "day3"])

print(myvar)

Locate Row
As you can see from the result above, the DataFrame is like a table with rows and columns.
Pandas use the loc attribute to return one or more specified row(s).

Example
Return row 0:

# refer to the row index:


print([Link][0])

Read CSV File and Store in DataFrame


What is CSV?

CSV stands for Comma Separated Values.


It is a file format used to store tabular data (rows and columns) in plain text form.
Example of CSV file ([Link]):

Name,Age,Marks
Anu,18,85
Ravi,19,90
Meena,18,88
Reading CSV File in Pandas

To read a CSV file in Pandas, we use the function:

pd.read_csv()

Basic Syntax

import pandas as pd
df = pd.read_csv("[Link]")

pd → alias for pandas


read_csv() → function to read CSV file
df → DataFrame object where data is stored

Example

import pandas as pd
df = pd.read_csv("[Link]")
print(df)

This will read the CSV file and store the data in a DataFrame named df.

Important Parameters of read_csv()


1️header

Specifies row number to use as column names.

Syntax:
pd.read_csv("[Link]", header=0)

2️names
Assign your own column names.

Syntax:
pd.read_csv("[Link]", names=["Name", "Age", "Marks"])

3️index_col

Sets a column as index.


Syntax:
pd.read_csv("[Link]", index_col="Name")

4️usecols

Reads only selected columns.

Syntax:
pd.read_csv("[Link]", usecols=["Name", "Marks"])

5️skiprows

Skips specified rows.

Syntax:
pd.read_csv("[Link]", skiprows=1)

To View the Data


After loading the CSV file, we can view the data using built-in methods.

1️head() Method

The head() method is used to display the first few rows of the DataFrame.
By default, it returns the first 5 rows.
We can also specify the number of rows.

Example:

import pandas as pd
df = pd.read_csv('[Link]')
print([Link]()) # first 5 rows
print([Link](10)) # first 10 rows

The head() method returns the column headers and the specified number of rows starting
from the top.

2️tail() Method

The tail() method is used to display the last few rows of the DataFrame.
By default, it returns the last 5 rows.
We can also specify the number of rows.
Example:

print([Link]()) # last 5 rows


print([Link](3)) # last 3 rows

The tail() method returns the column headers and the specified number of rows starting
from the bottom.

JSON File
What is JSON?

JSON stands for JavaScript Object Notation.


It is a lightweight data format used to store and exchange data.

Data in JSON is stored in key–value pairs.

JSON is commonly used in:

• Web applications
• APIs
• Data exchange between systems

Example of JSON File ([Link])


[
{"Name": "Anu", "Age": 18, "Marks": 85},
{"Name": "Ravi", "Age": 19, "Marks": 90},
{"Name": "Meena", "Age": 18, "Marks": 88}
]

In JSON:
• Data is written inside { }
• Key and value are separated by :
• Items are separated by ,

Reading JSON File in Pandas


To read a JSON file in Pandas, we use:

pd.read_json()
Syntax

import pandas as pd
df = pd.read_json('[Link]')

Here:

• read_json() reads the JSON file


• df stores the data in a DataFrame

Example Program

import pandas as pd
df = pd.read_json('[Link]')
print(df)

The JSON data will be converted into a DataFrame.

Writing JSON File


We can also convert a DataFrame into a JSON file using:

df.to_json('[Link]')

Viewing JSON Data


After reading the file:

print([Link]())
print([Link]())
print([Link]())

Matplotlib
• Creating simple plots is a common step in data visualization.
• These visual representations help us to understand trends, patterns and relationships within
data.
• Matplotlib is one of the most popular plotting libraries in Python which makes it easy to
generate high-quality graphs with just a few lines of code.
• To start creating plots we need to install Matplotlib using command

pip install matplotlib


Plotting x and y points

• The plot() function is used to draw points (markers) in a diagram.


• By default, the plot() function draws a line from point to point.
• The function takes parameters for specifying points in the diagram.
• Parameter 1 is an array containing the points on the x-axis.
• Parameter 2 is an array containing the points on the y-axis.

Example

import [Link] as plt


x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
[Link](x, y)
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Simple Plot')
[Link]()

Plotting Without Line


To plot only the markers, you can use shortcut string notation parameter 'o', which means
"rings".

Example:

Draw two points in the diagram, one at position (1, 3) and one in position (8, 10):

import [Link] as plt


import numpy as np

xpoints = [Link]([1, 8])


ypoints = [Link]([3, 10])

[Link](xpoints, ypoints, 'o')


[Link]()
Default X-Points
If we do not specify the points on the x-axis, they will get the default values 0, 1, 2, 3 etc.,
depending on the length of the y-points.

Example

Plotting without x-points:

import [Link] as plt


import numpy as np

ypoints = [Link]([3, 8, 1, 10, 5, 7])

[Link](ypoints)
[Link]()

Plotting a Scatter Plot


Scatter plots are basic type of plot used to visualize the relationship between two variables.

import [Link] as plt

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
[Link](x, y, color='blue', marker='x')

[Link]('X-axis')
[Link]('Y-axis')
[Link]('Simple Scatter Plot')
[Link]()

Application Example using Pandas


1. Data Cleaning and Preparation

Data cleaning is the process of removing errors, missing values, and inconsistencies from
data to make it suitable for analysis.

🔹

Why Data Cleaning is Important?
Real-world data is often incomplete or messy.
• Clean data improves accuracy of analysis.
• Helps in better decision making.

Handling Missing Values


1️Check for Missing Values

import pandas as pd
df = pd.read_csv("[Link]")
print([Link]())
print([Link]().sum())

2️Remove Missing Values

df = [Link]()

Removes rows with missing values.

3️Fill Missing Values

df = [Link](0)

Replace missing values with 0.

Or replace with mean value:

df["Marks"] =
df["Marks"].fillna(df["Marks"].mean())

Removing Duplicate Values

df = df.drop_duplicates()

Changing Data Types

df["Age"] = df["Age"].astype(int)

Renaming Columns

df = [Link](columns={"old_name": "new_name"})
Sorting Data

df = df.sort_values("Marks")

2. Data Visualization
Data Visualization means representing data in graphical form to understand patterns and
trends.
We use Matplotlib with Pandas for visualization.

🔹 Line Plot

import [Link] as plt


[Link](x="Name", y="Marks")
[Link]()

🔹 Bar Chart

[Link](kind="bar", x="Name",
y="Marks")
[Link]()
🔹 Histogram

df["Marks"].plot(kind="hist")
[Link]()

🔹 Scatter Plot

[Link](kind="scatter", x="Age",
y="Marks")
[Link]()

You might also like