📘 PAPER 2: OBJECT ORIENTED PROGRAMMING WITH PYTHON UNIT 5 – NumPy and Pandas (university of allahabad)

 

🔴 UNIT 5 – NumPy and Pandas


🟦 PART A: NUMPY (Numerical Python)


1️⃣ Introduction to NumPy

✅ What is NumPy?

NumPy is a Python library used for:

  • Numerical computation

  • Scientific computing

  • Working with arrays and matrices

✅ Features of NumPy

✔ Faster than Python lists
✔ Supports multi-dimensional arrays
✔ Efficient memory usage
✔ Used in ML, AI, Data Science


2️⃣ ndarray (N-Dimensional Array)

Definition:

The main object of NumPy is ndarray, which represents a multi-dimensional array.

Example:

import numpy as np a = np.array([1,2,3]) print(a)

3️⃣ Data Types in NumPy

a = np.array([1,2,3], dtype=float)

Common Data Types:

  • int

  • float

  • bool

  • complex


4️⃣ Array Attributes

AttributeDescription
ndimNumber of dimensions
shapeSize of array
sizeTotal elements
dtypeData type

Example:

a = np.array([[1,2],[3,4]]) print(a.ndim) print(a.shape)

5️⃣ Array Creation Routines

🔹 From List

np.array([1,2,3])

🔹 Zeros & Ones

np.zeros((2,2)) np.ones((3,3))

🔹 Using arange()

np.arange(1,10,2)

🔹 Using linspace()

np.linspace(1,10,5)

6️⃣ Array from Existing Data

np.asarray([1,2,3]) np.frombuffer(b'hello', dtype='S1')

7️⃣ Array Indexing & Slicing

a = np.array([10,20,30,40]) print(a[1]) print(a[1:3])

8️⃣ Mathematical Operations

a + b a * b np.sqrt(a) np.sum(a) np.mean(a)

🟩 PART B: PANDAS


9️⃣ Introduction to Pandas

✅ What is Pandas?

Pandas is a Python library used for:

  • Data analysis

  • Data manipulation

  • Handling structured data


🔟 Pandas Data Structures

1️⃣ Series

A one-dimensional labeled array.

import pandas as pd s = pd.Series([10,20,30])

2️⃣ DataFrame

A two-dimensional table-like structure.

data = { "Name": ["Amit", "Rahul"], "Marks": [80, 90] } df = pd.DataFrame(data)

1️⃣1️⃣ Creating Series

From List

pd.Series([1,2,3])

From Dictionary

pd.Series({'a':10, 'b':20})

From Scalar

pd.Series(5, index=[1,2,3])

1️⃣2️⃣ Creating DataFrame

From List

pd.DataFrame([[1,2],[3,4]])

From Dictionary

pd.DataFrame({ "Name":["A","B"], "Age":[20,22] })

1️⃣3️⃣ Manipulating DataFrames

Rename Column

df.rename(columns={"Name":"Student_Name"})

Delete Column

df.drop("Age", axis=1)

Delete Row

df.drop(0)

1️⃣4️⃣ Handling Missing Values

Finding Missing Values

df.isnull()

Filling Missing Values

df.fillna(0)

Dropping Missing Values

df.dropna()

1️⃣5️⃣ Advantages of Pandas

✔ Easy data handling
✔ Fast processing
✔ Data cleaning
✔ Used in ML & AI

Comments

Popular Posts