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

Module 5 App

The document provides a comprehensive guide on creating and manipulating arrays using NumPy, including syntax for 1D, 2D, and 3D arrays, as well as operations involving scalars and arrays. It covers indexing, slicing, transposition, and various array processing techniques such as arithmetic operations, mathematical functions, and aggregation. Additionally, it introduces basic input/output operations for arrays and gives an overview of the Pandas library for data manipulation and analysis.

Uploaded by

varsha311005
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 views91 pages

Module 5 App

The document provides a comprehensive guide on creating and manipulating arrays using NumPy, including syntax for 1D, 2D, and 3D arrays, as well as operations involving scalars and arrays. It covers indexing, slicing, transposition, and various array processing techniques such as arithmetic operations, mathematical functions, and aggregation. Additionally, it introduces basic input/output operations for arrays and gives an overview of the Pandas library for data manipulation and analysis.

Uploaded by

varsha311005
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

🔷 Creating Arrays in NumPy (with Syntax)

In NumPy, arrays are created using the [Link]() function and


other built-in functions like zeros, ones, arange, etc.

🔹 1. Creating a 1D Array
➤ Syntax:
[Link]([elements])

➤ Example:
import numpy as np

arr = [Link]([1, 2, 3, 4])


print(arr)

🔹 2. Creating a 2D Array (Matrix)


➤ Syntax:
[Link]([[row1], [row2]])

➤ Example:
arr = [Link]([[1, 2, 3],
[4, 5, 6]])
print(arr)

🔹 3. Creating a 3D Array
➤ Syntax:
[Link]([[[...]]])

➤ Example:
arr = [Link]([[[1, 2], [3, 4]],
[[5, 6], [7, 8]]])
print(arr)

🔹 4. Array of Zeros
➤ Syntax:
[Link](shape)

➤ Example:
arr = [Link]((2, 3))
print(arr)

🔹 5. Array of Ones
➤ Syntax:
[Link](shape)

➤ Example:
arr = [Link]((3, 3))
print(arr)

🔹 6. Array using arange()


➤ Syntax:
[Link](start, stop, step)

➤ Example:
arr = [Link](1, 10, 2)
print(arr)

🔹 7. Array using linspace()


➤ Syntax:
[Link](start, stop, num)

➤ Example:
arr = [Link](1, 10, 5)
print(arr)

🔹 8. Identity Matrix
➤ Syntax:
[Link](n)

➤ Example:
arr = [Link](3)
print(arr)

🔹 9. Random Arrays
➤ Syntax:
[Link](shape)

➤ Example:
arr = [Link](2, 2)
print(arr)

🔷 Quick Summary Table


Method Purpose

array() Create normal arrays

zeros() Array filled with 0

ones() Array filled with 1

arange() Sequence with step

linspace() Evenly spaced values

eye() Identity matrix

[Link]() Random values


🔷 Using Arrays and Scalars in NumPy (with Syntax)
In NumPy, arrays and scalars are used together to perform
vectorized operations, which means operations are applied to every
element without using loops.

🔹 What is a Scalar?
A scalar is a single numerical value.

➤ Example:
5
10
3.14

🔹 What is an Array?
An array is a collection of elements (1D, 2D, etc.).

➤ Example:
[1, 2, 3]

🔷 1. Scalar + Array Operation


👉 The scalar is applied to each element of the array
➤ Syntax:
array + scalar
array - scalar
array * scalar
array / scalar

➤ Example:
import numpy as np

arr = [Link]([1, 2, 3])

print(arr + 5) # Add 5 to each element


print(arr * 2) # Multiply each element by 2

🔷 2. Scalar Subtraction
➤ Syntax:
array - scalar

➤ Example:
arr = [Link]([10, 20, 30])

print(arr - 5)

🔷 3. Scalar Division
➤ Syntax:
array / scalar

➤ Example:
arr = [Link]([10, 20, 30])

print(arr / 2)
🔷 4. Scalar Power Operation
➤ Syntax:
array ** scalar

➤ Example:
arr = [Link]([1, 2, 3])

print(arr ** 2) # Square each element

🔷 5. Array + Array Operation


👉 Operations are done element-wise
➤ Syntax:
array1 + array2
array1 - array2
array1 * array2
array1 / array2

➤ Example:
a = [Link]([1, 2, 3])
b = [Link]([4, 5, 6])

print(a + b)
print(a * b)

🔷 6. Mixed Example (Array + Scalar + Array)


➤ Example:
arr = [Link]([2, 4, 6])
result = arr * 2 + 3
print(result)

👉 Step-by-step:
●​ Multiply each element by 2 → [4, 8, 12]
●​ Add 3 → [7, 11, 15]

🔷 Key Concept (Important for Exams)


✔ Scalar = single value​
✔ Array = multiple values​
✔ NumPy applies scalar operations to each element automatically​
✔ This is called vectorization

🔷 Advantages of Scalar + Array Operations


●​ 🚀 Faster than loops
●​ 🧠 Easy to write and read
●​ 💾 Memory efficient
●​ 🔁 Automatically applied to all elements
🔷 Indexing in NumPy Arrays (with Syntax)
Indexing means accessing individual elements from a NumPy array
using their position (index).

👉 NumPy indexing starts from 0 (same as Python lists).

🔹 1. Indexing in 1D Array
➤ Syntax:
array[index]

➤ Example:
import numpy as np

arr = [Link]([10, 20, 30, 40])

print(arr[0]) # First element


print(arr[2]) # Third element

🔹 2. Negative Indexing
👉 Used to access elements from the end
➤ Syntax:
array[-index]

➤ Example:
arr = [Link]([10, 20, 30, 40])

print(arr[-1]) # Last element


print(arr[-2]) # Second last

🔹 3. Indexing in 2D Array (Matrix)


👉 Format:
array[row_index][column_index]

or

array[row_index, column_index]

➤ Example:
arr = [Link]([[1, 2, 3],
[4, 5, 6]])

print(arr[0, 1]) # Row 0, Column 1 → 2


print(arr[1, 2]) # Row 1, Column 2 → 6

🔹 4. Indexing in 3D Array
➤ Syntax:
array[block][row][column]

➤ Example:
arr = [Link]([[[1, 2],
[3, 4]],

[[5, 6],
[7, 8]]])

print(arr[0, 1, 1]) # Output: 4


print(arr[1, 0, 0]) # Output: 5

🔷 Slicing in NumPy Arrays


Slicing means extracting a part of the array

🔹 5. Slicing in 1D Array
➤ Syntax:
array[start:stop:step]

➤ Example:
arr = [Link]([10, 20, 30, 40, 50])

print(arr[1:4]) # [20 30 40]


print(arr[:3]) # [10 20 30]
print(arr[::2]) # [10 30 50]

🔹 6. Slicing in 2D Array
➤ Syntax:
array[row_start:row_end, col_start:col_end]

➤ Example:
arr = [Link]([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

print(arr[0:2, 1:3])
👉 Output:
[[2 3]
[5 6]]

🔷 Key Points (Exam Important)


✔ Index starts from 0​
✔ Negative index starts from -1 (last element)​
✔ 2D arrays use row, column format​
✔ Slicing extracts a sub-part of array

🔷 Quick Summary
Concept Syntax

1D indexing array[i]

Negative indexing array[-i]

2D indexing array[row, col]

1D slicing array[start:stop]

2D slicing array[r1:r2, c1:c2]


🔷 Array Transposition in NumPy
Transpose of an array means interchanging rows and columns.

👉 In simple words:​
Rows become columns and columns become rows.

🔹 1. Transpose of 1D Array
👉 A 1D array does not change on transpose.
➤ Syntax:
array.T

➤ Example:
import numpy as np

arr = [Link]([1, 2, 3, 4])

print(arr.T)

➤ Output:
[1 2 3 4]

🔹 2. Transpose of 2D Array (Matrix)


👉 Rows become columns
➤ Syntax:
array.T

➤ Example:
arr = [Link]([[1, 2, 3],
[4, 5, 6]])

print(arr.T)

➤ Output:
[[1 4]
[2 5]
[3 6]]

🔹 3. Using [Link]()
👉 Another method to transpose an array
➤ Syntax:
[Link](array)

➤ Example:
arr = [Link]([[10, 20],
[30, 40],
[50, 60]])

print([Link](arr))

➤ Output:
[[10 30 50]
[20 40 60]]

🔹 4. Transpose of 3D Array
👉 Changes axes order
➤ Syntax:
array.T

➤ Example:
arr = [Link]([[[1, 2],
[3, 4]],

[[5, 6],
[7, 8]]])

print(arr.T)

➤ Output:
[[[1 5]
[3 7]]

[[2 6]
[4 8]]]

🔷 Key Points (Exam Important)


✔ Transpose = rows ↔ columns​
✔ Works mainly on 2D and 3D arrays​
✔ Syntax:

●​ array.T
●​ [Link](array)​
✔ 1D array does not change

🔷 Quick Summary
Method Syntax Purpose

Attribute array.T Simple transpose

Function [Link](a Flexible transpose


rray)
🔷 Array Processing in NumPy
Array processing in NumPy means performing operations on arrays
such as:

●​ arithmetic operations
●​ mathematical functions
●​ transformations
●​ aggregation (sum, mean, etc.)
●​ element-wise processing

👉 NumPy processes arrays without loops, using fast vectorized


operations.

🔹 1. Element-wise Operations (Core of Array Processing)


👉 Operations are applied to each element automatically.
➤ Syntax:
array + value
array - value
array * value
array / value

➤ Example:
import numpy as np

arr = [Link]([1, 2, 3, 4])

print(arr + 10)
print(arr * 2)

➤ Output:
[11 12 13 14]
[2 4 6 8]

🔹 2. Array-to-Array Processing
👉 Same position elements are processed together
➤ Syntax:
array1 + array2
array1 * array2

➤ Example:
a = [Link]([1, 2, 3])
b = [Link]([4, 5, 6])

print(a + b)
print(a * b)

➤ Output:
[5 7 9]
[4 10 18]

🔹 3. Mathematical Processing
NumPy provides built-in math functions.

➤ Syntax:
[Link](array)

➤ Example:
arr = [Link]([1, 4, 9, 16])
print([Link](arr)) # square root
print([Link](arr)) # logarithm

🔹 4. Aggregation Processing
👉 Used to summarize data
➤ Syntax:
[Link](array)
[Link](array)
[Link](array)
[Link](array)

➤ Example:
arr = [Link]([10, 20, 30, 40])

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

➤ Output:
100
25.0

🔹 5. Reshaping Processing
👉 Changes shape of array without changing data
➤ Syntax:
[Link](rows, columns)
➤ Example:
arr = [Link]([1, 2, 3, 4, 5, 6])

print([Link](2, 3))

➤ Output:
[[1 2 3]
[4 5 6]]

🔹 6. Filtering (Boolean Processing)


👉 Used to select elements based on condition
➤ Syntax:
array[condition]

➤ Example:
arr = [Link]([10, 15, 20, 25, 30])

print(arr[arr > 20])

➤ Output:
[25 30]

🔹 7. Broadcasting (Important Concept)


👉 Allows operations on different shaped arrays
➤ Example:
arr = [Link]([1, 2, 3])
print(arr + 5)

➤ Output:
[6 7 8]

🔷 Key Points (Exam Important)


✔ No loops needed​
✔ Fast execution​
✔ Works element-wise​
✔ Supports math, logic, statistics​
✔ Uses broadcasting for flexibility

🔷 Summary Table
Type Example

Arithmetic arr + 5

Array ops a + b

Math [Link]()

Aggregation [Link]()

Reshape reshape()

Filtering arr[arr > x]


🔷 Array Input and Output in NumPy
In NumPy, input means taking array values from the user, and output
means displaying or saving array data.

🔹 1. Array Input in NumPy


NumPy does not have a special input function like lists. We usually
take input using Python input() and convert it into a NumPy array.

➤ Method 1: Using input() and list()

🔹 Syntax:
[Link](list(input().split()))

🔹 Example:
import numpy as np

arr = [Link](list(map(int, input("Enter numbers: ").split())))

print(arr)

🔹 Input:
Enter numbers: 10 20 30 40

🔹 Output:
[10 20 30 40]
➤ Method 2: Taking 2D Array Input

🔹 Syntax:
[Link]([list(map(int, input().split())), ...])

🔹 Example:
import numpy as np

rows = int(input("Enter rows: "))

arr = [Link]([list(map(int, input().split())) for i in range(rows)])

print(arr)

🔹 Input:
Enter rows: 2
123
456

🔹 Output:
[[1 2 3]
[4 5 6]]

🔹 2. Array Output in NumPy


Array output means displaying the array or saving it.

➤ Simple Output

🔹 Syntax:
print(array)

🔹 Example:
import numpy as np

arr = [Link]([1, 2, 3])


print(arr)

➤ Formatted Output

🔹 Syntax:
print([Link])
print([Link])
print([Link])

🔹 Example:
arr = [Link]([[1, 2], [3, 4]])

print("Array:\n", arr)
print("Shape:", [Link])
print("Dimensions:", [Link])

🔹 3. Saving Array Output to File


🔹 Syntax:
[Link]("[Link]", array)

🔹 Example:
arr = [Link]([10, 20, 30])

[Link]("[Link]", arr)
🔹 4. Loading Saved Array
🔹 Syntax:
[Link]("[Link]")

🔹 Example:
data = [Link]("[Link]")
print(data)

🔷 Key Points (Exam Important)


✔ Input is usually taken using input() + map()​
✔ Arrays are created using [Link]()​
✔ Output is displayed using print()​
✔ NumPy supports saving/loading arrays using .npy files​
✔ Very useful for data handling in ML and Data Science

🔷 Summary
Operation Syntax

1D Input [Link](list(map(int,
input().split())))

2D Input [Link]([list(map(int,
input().split())) for i in range(n)])

Output print(array)
Save [Link]()

Load [Link]()
🔷 Introduction to Pandas
🔹 What is Pandas?
Pandas is a powerful Python library used for data manipulation and
data analysis.

It provides easy-to-use data structures for working with structured


data (tables, rows, columns).

👉 In simple words:​
Pandas helps you work with Excel-like data in Python.

🔹 Why do we use Pandas?


We use Pandas because:

📊 It handles tabular data (rows and columns) easily


⚡ Faster data processing compared to manual methods
●​

🧹 Helps in data cleaning and preprocessing


●​

📈 Useful for data analysis and visualization


●​

🔍 Supports filtering, grouping, and sorting data easily


●​

🤖 Widely used in Data Science and Machine Learning


●​
●​

🔹 Installation of Pandas
You can install Pandas using pip:

pip install pandas

🔹 Importing Pandas
import pandas as pd

🔹 Core Data Structures in Pandas


Pandas mainly has two important data structures:

🔸 1. Series (1D Data)


A Series is like a column in Excel.

➤ Syntax:
[Link](data)

➤ Example:
import pandas as pd

s = [Link]([10, 20, 30, 40])


print(s)

🔸 2. DataFrame (2D Data)


A DataFrame is like a table (rows + columns).

➤ Syntax:
[Link](data)

➤ Example:
import pandas as pd

data = {
"Name": ["Asha", "Rahul", "Kiran"],
"Age": [20, 21, 22]
}
df = [Link](data)
print(df)

🔹 Features of Pandas
●​ 📌 Easy handling of missing data
●​ 📌 Fast operations on large datasets
●​ 📌 Supports data alignment
●​ 📌 Powerful indexing and slicing
●​ 📌 Built-in functions for analysis

🔹 Where do we use Pandas?


Pandas is used in:

📊 Data Science
🤖 Machine Learning
●​

📈 Financial Analysis
●​

📉 Stock Market Analysis


●​

🧹 Data Cleaning (preprocessing)


●​

📑 Excel and CSV file handling


●​
●​

🔹 Applications of Pandas
1. Data Analysis

Used to analyze large datasets easily.

2. Data Cleaning
Removes missing or incorrect data.

3. Data Visualization Support

Works with libraries like Matplotlib and Seaborn.

4. File Handling

Reads and writes files like:

●​ CSV
●​ Excel
●​ JSON

5. Machine Learning Preprocessing

Prepares data before training models.

🔷 Summary
Concept Meaning

Pandas Data analysis library

Series 1D labeled data

DataFrame 2D table-like data

Use Data cleaning, analysis, ML

👉 Final Idea:​
Pandas = Python tool for working with structured data like Excel
sheets
🔷 Series in Pandas
🔹 What is a Series?
A Series is a one-dimensional labeled array in Pandas.

👉 It can store:
●​ integers
●​ floats
●​ strings
●​ Python objects

👉 Think of it as a single column in an Excel sheet.


🔹 Structure of a Series
A Series has two parts:

📌 Index → label for each element


📌 Data values → actual values
●​
●​

🔹 Syntax of Series
[Link](data, index)

🔹 Creating a Series
🔸 1. From List
import pandas as pd

s = [Link]([10, 20, 30, 40])


print(s)

🔹 Output:
0 10
1 20
2 30
3 40
dtype: int64

🔸 2. Series with Custom Index


import pandas as pd

s = [Link]([100, 200, 300], index=["a", "b", "c"])


print(s)

🔹 Output:
a 100
b 200
c 300
dtype: int64

🔸 3. Series from Dictionary


import pandas as pd

data = {"Math": 90, "Science": 85, "English": 88}

s = [Link](data)
print(s)

🔹 Output:
Math 90
Science 85
English 88
dtype: int64

🔹 Accessing Elements in Series


🔸 Using Index Position
s = [Link]([10, 20, 30])

print(s[0])

🔸 Using Label Index


s = [Link]([100, 200, 300], index=["a", "b", "c"])

print(s["b"])

🔹 Slicing in Series
🔸 Syntax:
series[start:end]

🔸 Example:
s = [Link]([10, 20, 30, 40, 50])

print(s[1:4])

🔹 Output:
1 20
2 30
3 40
dtype: int64

🔹 Operations on Series
🔸 1. Arithmetic Operations
s = [Link]([1, 2, 3])

print(s + 10)
print(s * 2)

🔸 2. Mathematical Functions
import numpy as np

s = [Link]([1, 4, 9, 16])

print([Link](s))

🔹 Key Features of Series


●​ 📌 One-dimensional data structure
●​ 📌 Has labeled index
●​ 📌 Supports different data types
●​ 📌 Works like a column in a table
●​ 📌 Supports vectorized operations

🔹 Where Series is Used?


●​ 📊 Data analysis of single columns
●​ 📈 Statistical calculations
🧹 Data cleaning
🤖 Machine learning preprocessing
●​
●​

🔷 Summary
Featur Description
e

Series 1D labeled array

Index Labels for data

Data int, float, string


types

Use Column-like data


handling

👉 Final Idea:​
A Series = one column of data with labels (index) in Pandas.
🔷 DataFrame in Pandas
🔹 What is a DataFrame?
A DataFrame is a 2-dimensional labeled data structure in Pandas.

👉 It is like a table in Excel with:


●​ 📌 Rows
●​ 📌 Columns

Each column in a DataFrame is a Series.

🔹 Syntax of DataFrame
[Link](data)

🔹 Creating a DataFrame
🔸 1. From Dictionary
import pandas as pd

data = {
"Name": ["Asha", "Rahul", "Kiran"],
"Age": [20, 21, 22],
"Marks": [85, 90, 88]
}

df = [Link](data)
print(df)
🔹 Output:
Name Age Marks
0 Asha 20 85
1 Rahul 21 90
2 Kiran 22 88

🔸 2. DataFrame with Custom Index


import pandas as pd

data = {
"Name": ["Asha", "Rahul"],
"Age": [20, 21]
}

df = [Link](data, index=["A", "B"])


print(df)

🔹 Output:
Name Age
A Asha 20
B Rahul 21

🔸 3. From List of Lists


import pandas as pd

data = [
["Asha", 20],
["Rahul", 21],
["Kiran", 22]
]
df = [Link](data, columns=["Name", "Age"])
print(df)

🔸 4. From Series
import pandas as pd

s1 = [Link]([1, 2, 3])
s2 = [Link]([4, 5, 6])

df = [Link]({"Col1": s1, "Col2": s2})


print(df)

🔹 Accessing Data in DataFrame


🔸 1. Column Access
df["Name"]

👉 Example:
print(df["Name"])

🔸 2. Multiple Columns
df[["Name", "Age"]]

🔸 3. Row Access using iloc (index-based)


[Link][0]
👉 First row
🔸 4. Row Access using loc (label-based)
[Link][0]

🔹 Basic DataFrame Operations


🔸 1. Head (first rows)
[Link]()

🔸 2. Tail (last rows)


[Link]()

🔸 3. Shape (rows, columns)


[Link]

🔸 4. Column Names
[Link]

🔸 5. Summary
[Link]()
🔹 Features of DataFrame
●​ 📊 2D tabular structure
●​ 📌 Rows and columns
●​ ⚡ Fast data processing
●​ 🧹 Easy data cleaning
●​ 🔍 Powerful indexing

🔹 Where DataFrame is Used?


●​ 📊 Data Science
●​ 🤖 Machine Learning
●​ 📈 Financial analysis
●​ 🧹 Data cleaning
●​ 📑 Excel/CSV file handling

🔷 Summary
Feature Description

DataFra 2D table in
me Pandas

Structure Rows +
Columns

Column Series
type

Use Data analysis


and ML
🔷 Final Idea
👉 DataFrame = Table-like structure (like Excel) used for data
analysis in Python

🔷 Index Objects in Pandas


🔹 What is an Index Object?
In Pandas, an Index object is used to label and identify rows and
columns in Series and DataFrame.

👉 It acts like an address system for data.


🔹 Simple Meaning
●​ Every row in a DataFrame has an index
●​ Every column also has a label (column index)
●​ These labels are stored in an object called Index

🔹 Syntax
[Link]
[Link]

🔹 Example of Index in Series


import pandas as pd

s = [Link]([10, 20, 30], index=["a", "b", "c"])

print([Link])

🔹 Output:
Index(['a', 'b', 'c'], dtype='object')

🔹 Example of Index in DataFrame


import pandas as pd
data = {
"Name": ["Asha", "Rahul"],
"Age": [20, 21]
}

df = [Link](data)

print([Link])

🔹 Output:
RangeIndex(start=0, stop=2, step=1)

🔹 Types of Index Objects


🔸 1. Default Index
Automatically created by Pandas.

df = [Link]([10, 20, 30])


print([Link])

🔸 2. Custom Index
We can define our own index.

df = [Link]([10, 20, 30], index=["a", "b", "c"])


print([Link])

🔸 3. RangeIndex
Used when index is numeric sequence.

RangeIndex(start=0, stop=3, step=1)


🔹 Accessing Index Values
➤ Syntax:
[Link]

➤ Example:
print([Link][0])

🔹 Changing Index
➤ Syntax:
[Link] = new_index

➤ Example:
[Link] = ["x", "y", "z"]
print(df)

🔹 Resetting Index
➤ Syntax:
df.reset_index()

➤ Example:
df.reset_index()

🔹 Setting a Column as Index


➤ Syntax:
df.set_index("column_name")

➤ Example:
df.set_index("Name")

🔹 Key Features of Index Objects


●​ 📌 Immutable (cannot be changed directly)
●​ 📌 Helps in fast data access
●​ 📌 Used for alignment of data
●​ 📌 Supports labeling in Series and DataFrame

🔹 Where Index is Used?


●​ 📊 Data alignment in operations
●​ 🔍 Fast data selection
●​ 📑 Row/column identification
●​ 🤖 Machine learning preprocessing
●​ 📈 Data analysis and filtering

🔷 Summary
Concept Meaning

Index Row/column labels

[Link] Row labels

[Link] Column labels

RangeIndex Default numeric index


set_index Set custom index

🔷 Final Idea
👉 Index object = label system for rows and columns in Pandas
It helps Pandas organize, access, and manage data efficiently.
🔷 Reindex in Pandas
🔹 What is Reindex?
Reindexing in Pandas means changing the existing index of a
Series or DataFrame to a new set of labels.

👉 It helps to:
●​ add new index labels
●​ remove old ones
●​ rearrange data order

🔹 Why do we use Reindex?


●​ 📌 To change the structure of data indexing
●​ 📌 To align data with new labels
●​ 📌 To introduce missing values (NaN) for new indexes
●​ 📌 To reorder rows or columns

🔹 Syntax of Reindex
➤ For Series:
[Link](new_index)

➤ For DataFrame:
[Link](new_index)
[Link](columns=new_columns)

🔹 1. Reindex in Series
🔸 Example:
import pandas as pd

s = [Link]([10, 20, 30], index=["a", "b", "c"])

s2 = [Link](["a", "b", "c", "d"])


print(s2)

🔹 Output:
a 10.0
b 20.0
c 30.0
d NaN
dtype: float64

👉 New index "d" gives NaN (Not a Number) because no value


exists.

🔹 2. Reindex in DataFrame (Rows)


🔸 Example:
import pandas as pd

data = {
"Name": ["Asha", "Rahul", "Kiran"],
"Age": [20, 21, 22]
}

df = [Link](data, index=["a", "b", "c"])

df2 = [Link](["a", "b", "c", "d"])


print(df2)

🔹 Output:
Name Age
a Asha 20.0
b Rahul 21.0
c Kiran 22.0
d NaN NaN

🔹 3. Reindex Columns in DataFrame


🔸 Example:
df2 = [Link](columns=["Age", "Name", "Marks"])
print(df2)

🔹 Output:
Age Name Marks
a 20.0 Asha NaN
b 21.0 Rahul NaN
c 22.0 Kiran NaN

🔹 4. Filling Missing Values in Reindex


👉 We can replace NaN values using fill_value
➤ Syntax:
reindex(index, fill_value=value)

🔸 Example:
s = [Link]([10, 20, 30], index=["a", "b", "c"])
s2 = [Link](["a", "b", "c", "d"], fill_value=0)
print(s2)

🔹 Output:
a 10
b 20
c 30
d 0
dtype: int64

🔹 Key Points of Reindex


●​ 📌 Changes index labels
●​ 📌 Missing values become NaN
●​ 📌 Can reorder data
●​ 📌 Can add new rows/columns
●​ 📌 Useful in data alignment

🔷 Summary
Feature Description

reindex() Changes index


structure

New index Adds new labels

Missing Filled with NaN


values

fill_value Replaces NaN


🔷 Final Idea
👉 Reindex = changing the structure/order of index labels in
Pandas

It is mainly used for data alignment and restructuring datasets.


🔷 Drop Entry in Pandas
🔹 What is Drop Entry?
Drop entry in Pandas means removing rows or columns from a
Series or DataFrame.

👉 It is used to delete unwanted data.

🔹 Why do we use Drop?


●​ 🧹 To remove unnecessary rows/columns
●​ 📊 To clean data
●​ ❌ To delete missing or invalid data
●​ ⚡ To reduce dataset size

🔹 Syntax of Drop
➤ For Series / DataFrame (rows or columns):
[Link](labels, axis)

🔹 Meaning of Parameters
Parame Meaning
ter

labels row/column name to


remove

axis=0 row deletion


axis=1 column deletion

🔹 1. Drop Rows in DataFrame


➤ Syntax:
[Link](index)

➤ Example:
import pandas as pd

data = {
"Name": ["Asha", "Rahul", "Kiran"],
"Age": [20, 21, 22]
}

df = [Link](data)

df2 = [Link](1) # remove row at index 1


print(df2)

🔹 Output:
Name Age
0 Asha 20
2 Kiran 22

🔹 2. Drop Multiple Rows


➤ Syntax:
[Link]([index1, index2])
➤ Example:
df2 = [Link]([0, 2])
print(df2)

🔹 Output:
Name Age
1 Rahul 21

🔹 3. Drop Columns in DataFrame


➤ Syntax:
[Link](column_name, axis=1)

➤ Example:
df2 = [Link]("Age", axis=1)
print(df2)

🔹 Output:
Name
0 Asha
1 Rahul
2 Kiran

🔹 4. Drop Multiple Columns


➤ Example:
df2 = [Link](["Name", "Age"], axis=1)
print(df2)
🔹 5. Drop in Series
➤ Syntax:
[Link](index)

➤ Example:
s = [Link]([10, 20, 30], index=["a", "b", "c"])

s2 = [Link]("b")
print(s2)

🔹 Output:
a 10
c 30
dtype: int64

🔹 6. Inplace Drop (Modify Original Data)


➤ Syntax:
[Link](..., inplace=True)

➤ Example:
[Link](1, inplace=True)
print(df)

👉 This directly modifies the original DataFrame.

🔹 Key Points
●​ 📌 axis=0 → rows
●​ 📌 axis=1 → columns
●​ 📌 drop() removes data
●​ 📌 inplace=True modifies original data
●​ 📌 Used for data cleaning

🔷 Summary
Operation Syntax

Drop row [Link](index)

Drop column [Link](col, axis=1)

Drop multiple [Link]([..])

Modify original inplace=True

🔷 Final Idea
👉 Drop entry = removing unwanted rows or columns from
Pandas data

It is very important for data cleaning in Data Science.


🔷 Select Entries in Pandas
🔹 What is Selection of Entries?
Selecting entries in Pandas means accessing specific rows,
columns, or values from a Series or DataFrame.

👉 It is used to extract required data from a dataset.

🔹 Why do we use Selection?


●​ 📊 To view specific data
●​ 🔍 To filter required rows/columns
●​ ⚡ To access values quickly
●​ 🧠 Important for data analysis and ML

🔹 1. Selecting Entries in Series


🔸 1.1 Using Index Label
➤ Syntax:
series["label"]

➤ Example:
import pandas as pd

s = [Link]([10, 20, 30], index=["a", "b", "c"])

print(s["b"])

🔹 Output:
20

🔸 1.2 Using Position Index


➤ Syntax:
series[index]

➤ Example:
print(s[1])

🔹 Output:
20

🔸 1.3 Slicing in Series


➤ Syntax:
series[start:end]

➤ Example:
print(s[0:2])

🔹 Output:
a 10
b 20
dtype: int64

🔹 2. Selecting Entries in DataFrame


🔸 2.1 Selecting Column
➤ Syntax:
df["column_name"]

➤ Example:
import pandas as pd

data = {
"Name": ["Asha", "Rahul", "Kiran"],
"Age": [20, 21, 22]
}

df = [Link](data)

print(df["Name"])

🔹 Output:
0 Asha
1 Rahul
2 Kiran
Name: Name, dtype: object

🔸 2.2 Selecting Multiple Columns


➤ Syntax:
df[["col1", "col2"]]

➤ Example:
print(df[["Name", "Age"]])

🔸 2.3 Selecting Rows using loc


👉 Used for label-based selection
➤ Syntax:
[Link][row_label]

➤ Example:
df = [Link](data, index=["a", "b", "c"])

print([Link]["b"])

🔹 Output:
Name Rahul
Age 21
Name: b, dtype: object

🔸 2.4 Selecting Rows using iloc


👉 Used for position-based selection
➤ Syntax:
[Link][index]

➤ Example:
print([Link][1])

🔹 Output:
Name Rahul
Age 21
Name: b, dtype: object

🔸 2.5 Selecting Specific Value


➤ Syntax:
[Link][row, column]

➤ Example:
print([Link]["b", "Name"])

🔹 Output:
Rahul

🔹 3. Boolean Selection (Filtering)


👉 Used to select data based on condition
➤ Syntax:
df[condition]

➤ Example:
print(df[df["Age"] > 20])

🔹 Output:
Name Age
b Rahul 21
c Kiran 22

🔷 Key Methods of Selection


Meth Use
od

[] Select column
loc[ Label-based
] selection

iloc Position-based
[] selection

Slicin Range selection


g

Boole Condition-based
an filtering

🔷 Final Idea
👉 Select Entries = extracting specific rows, columns, or values
from Pandas data

It is very important for:

📊 Data analysis
🧹 Data cleaning
●​

🤖 Machine learning preprocessing


●​
●​
🔷 Data Alignment in Pandas
🔹 What is Data Alignment?
Data alignment in Pandas means that when performing operations on
Series or DataFrames, Pandas automatically matches data based on
index labels, not position.

👉 In simple words:​
Pandas aligns data using index before doing calculations.

🔹 Why is Data Alignment Important?


●​ 📌 Ensures correct matching of data
●​ 📌 Avoids wrong calculations
●​ 📌 Handles missing values automatically (NaN)
●​ 📌 Works even if indexes are different

🔹 1. Data Alignment in Series


🔸 Example:
import pandas as pd

s1 = [Link]([10, 20, 30], index=["a", "b", "c"])


s2 = [Link]([5, 15, 25], index=["b", "c", "d"])

result = s1 + s2
print(result)
🔹 Output:
a NaN
b 25.0
c 45.0
d NaN
dtype: float64

🔹 Explanation:
Ind s s Res
ex 1 2 ult

a 1 - NaN
0

b 2 5 25
0

c 3 1 45
0 5

d - 2 NaN
5

👉 Pandas matches values using index labels

🔹 2. Data Alignment in DataFrame


🔸 Example:
df1 = [Link]({
"A": [1, 2, 3]
}, index=["x", "y", "z"])
df2 = [Link]({
"A": [10, 20, 30]
}, index=["y", "z", "w"])

result = df1 + df2


print(result)

🔹 Output:
A
w NaN
x NaN
y 12.0
z 32.0

🔹 Explanation:
●​ Matching indexes:
○​ y → 2 + 10 = 12
○​ z → 3 + 20 = 23 (wait correction below)

👉 Correct calculation:
Ind df df Res
ex 1 2 ult

x 1 - NaN

y 2 10 12

z 3 20 23

w - 30 NaN
🔹 3. Key Rule of Data Alignment
👉 Pandas always performs:
Match by index → then perform operation

NOT:

Match by position (like arrays)

🔹 4. Handling Missing Values in Alignment


When index does not match, Pandas assigns:

NaN (Not a Number)

➤ You can replace NaN:


[Link](0)

🔹 5. Data Alignment with Different Columns


df1 = [Link]({"A": [1, 2]}, index=["x", "y"])
df2 = [Link]({"B": [3, 4]}, index=["x", "y"])

print(df1 + df2)

🔹 Output:
A B
x NaN NaN
y NaN NaN
👉 Because columns are different → no match → NaN

🔹 6. Automatic Alignment Features


Pandas automatically:

●​ ✔ Aligns indexes
●​ ✔ Aligns columns
●​ ✔ Handles missing data
●​ ✔ Avoids manual mapping

🔷 Key Points (Exam Important)


●​ 📌 Data alignment is based on index labels
●​ 📌 Not based on position
●​ 📌 Missing matches → NaN
●​ 📌 Happens in Series and DataFrame
●​ 📌 Very important in data analysis

🔷 Summary
Concept Meaning

Data Matching data using


Alignment index

Result Matched values + NaN


for missing
Used in Series & DataFrame
operations

🔷 Final Idea
👉 Data Alignment = automatic matching of data using index
before performing operations
🔷 Rank and Sort in Pandas
In Pandas, Rank and Sort are used to arrange data in order and
assign positions based on value.

🔷 1. SORTING in Pandas
🔹 What is Sorting?
Sorting means arranging data in:

📈 Ascending order (small → large)


📉 Descending order (large → small)
●​
●​

🔹 Syntax of Sorting
➤ Series:
series.sort_values()

➤ DataFrame:
df.sort_values(by="column_name")

🔹 1. Sorting a Series
➤ Example:
import pandas as pd

s = [Link]([40, 10, 30, 20])

print(s.sort_values())
🔹 Output:
1 10
3 20
2 30
0 40
dtype: int64

🔹 2. Sorting DataFrame (Ascending)


➤ Example:
df = [Link]({
"Name": ["Asha", "Rahul", "Kiran"],
"Age": [22, 20, 21]
})

print(df.sort_values(by="Age"))

🔹 Output:
Name Age
1 Rahul 20
2 Kiran 21
0 Asha 22

🔹 3. Sorting in Descending Order


➤ Syntax:
df.sort_values(by="column", ascending=False)

➤ Example:
print(df.sort_values(by="Age", ascending=False))
🔷 2. RANKING in Pandas
🔹 What is Ranking?
Ranking assigns a position number to values based on their size.

👉 Small value → low rank​


👉 Large value → high rank
🔹 Syntax of Rank
➤ Series:
[Link]()

🔹 1. Basic Ranking Example


s = [Link]([50, 20, 40, 10])

print([Link]())

🔹 Output:
0 4.0
1 2.0
2 3.0
3 1.0
dtype: float64

🔹 2. Rank with Sorted Values


s = [Link]([50, 20, 40, 10])

print([Link](ascending=True))
🔹 3. Handling Duplicate Values in Rank
s = [Link]([10, 20, 20, 30])

print([Link]())

🔹 Output:
0 1.0
1 2.5
2 2.5
3 4.0
dtype: float64

👉 Duplicate values get average rank

🔷 Difference Between Rank and Sort


Feature Sort Rank

Purpose Arrange Assign


data position

Output Ordered Rank


values numbers

Change data Yes No


order

Duplicate Keeps Gives


handling order average rank
🔷 Key Parameters
Sorting:

●​ ascending=True/False
●​ by="column_name"

Ranking:

●​ ascending=True/False
●​ method="average|min|max|first"

🔷 Important Methods in Rank


Method Meaning

average average rank for duplicates

min lowest rank

max highest rank

first order of appearance

🔷 Final Idea
👉 Sort = arrange data​
👉 Rank = assign position to data
🔷 Summary Statistics in Pandas
🔹 What is Summary Statistics?
Summary statistics in Pandas means numerical values that
describe and summarize data.

👉 It helps us understand:
●​ central value
●​ spread of data
●​ distribution of data

🔹 Why do we use Summary Statistics?


●​ 📊 To understand dataset quickly
●​ 📈 To find average, max, min values
●​ 🧠 To analyze data patterns
●​ 📉 To detect outliers
●​ 🤖 Very important in Data Science & Machine Learning

🔷 1. Basic Summary Functions in Pandas


🔹 1. mean()
👉 Finds average value
➤ Syntax:
[Link]()

➤ Example:
import pandas as pd

df = [Link]({"Marks": [80, 90, 70, 85]})

print(df["Marks"].mean())

🔹 Output:
81.25

🔹 2. median()
👉 Middle value of dataset
➤ Syntax:
[Link]()

➤ Example:
print(df["Marks"].median())

🔹 3. mode()
👉 Most repeated value
➤ Syntax:
[Link]()

➤ Example:
print(df["Marks"].mode())

🔹 4. sum()
👉 Total of all values
➤ Syntax:
[Link]()

➤ Example:
print(df["Marks"].sum())

🔹 5. min() and max()


➤ Syntax:
[Link]()
[Link]()

➤ Example:
print(df["Marks"].min())
print(df["Marks"].max())

🔷 2. describe() Function (Most Important)


👉 Gives complete summary statistics in one function
🔹 Syntax:
[Link]()

🔹 Example:
df = [Link]({"Marks": [80, 90, 70, 85, 95]})
print([Link]())

🔹 Output:
Marks
count 5.000000
mean 84.000000
std 9.617
min 70.000000
25% 80.000000
50% 85.000000
75% 90.000000
max 95.000000

🔷 3. Meaning of describe() Output


Statis Meaning
tic

count number of
values

mean average

std standard
deviation

min minimum
value

25% first quartile

50% median
75% third quartile

max maximum
value

🔷 4. Standard Deviation (std)


👉 Measures spread of data
➤ Syntax:
[Link]()

🔷 5. Variance
👉 Measures how data is spread from mean
➤ Syntax:
[Link]()

🔷 6. Summary of All Functions


Function Meaning

mean() Average

median() Middle value

mode() Most frequent value

sum() Total

min() Smallest value


max() Largest value

std() Spread of data

var() Variance

describe() Full summary

🔷 Final Idea
👉 Summary Statistics = mathematical summary of dataset
It helps to:

📊 understand data quickly


📈 analyze trends
●​

🧠 support decision making


●​
●​
🔷 Missing Data in Pandas
🔹 What is Missing Data?
Missing data means values that are not present or undefined in a
dataset.

👉 In Pandas, missing values are represented as:


NaN (Not a Number)

🔹 Why does Missing Data occur?


●​ 📊 Data entry mistakes
●​ 📁 Incomplete data collection
●​ 🌐 Data from external sources (CSV, Excel, APIs)
●​ ❌ System or sensor errors
●​ 🧹 Data cleaning issues

🔹 How Pandas handles Missing Data


Pandas provides special functions to:

●​ detect missing values


●​ remove missing values
●​ fill missing values

🔷 1. Detect Missing Data


🔹 isnull()
👉 Returns True if value is missing
➤ Syntax:
[Link]()

➤ Example:
import pandas as pd
import numpy as np

df = [Link]({
"Name": ["Asha", "Rahul", None],
"Age": [20, [Link], 22]
})

print([Link]())

🔹 Output:
Name Age
0 False False
1 False True
2 True False

🔹 isnull().sum()
👉 Counts missing values
print([Link]().sum())

🔷 2. Drop Missing Data


🔹 dropna()
👉 Removes rows or columns with missing values
🔸 Drop rows with NaN
➤ Syntax:
[Link]()

➤ Example:
print([Link]())

🔹 Output:
Name Age
0 Asha 20.0

🔸 Drop columns with NaN


➤ Syntax:
[Link](axis=1)

🔷 3. Fill Missing Data


🔹 fillna()
👉 Replaces missing values with given value
🔸 Fill with constant value
➤ Syntax:
[Link](value)

➤ Example:
print([Link](0))

🔹 Output:
Name Age
0 Asha 20.0
1 Rahul 0.0
2 0 22.0

🔸 Fill with mean value


➤ Example:
df["Age"] = df["Age"].fillna(df["Age"].mean())
print(df)

🔷 4. Replace Missing Data


🔹 replace()
👉 Used to replace NaN or other values
➤ Syntax:
[Link](old_value, new_value)

➤ Example:
[Link]([Link], 50)

🔷 5. Forward Fill (ffill)


👉 Fills missing value using previous value
➤ Syntax:
[Link](method="ffill")

➤ Example:
[Link](method="ffill")

🔷 6. Backward Fill (bfill)


👉 Fills missing value using next value
➤ Syntax:
[Link](method="bfill")

🔷 Types of Missing Data Handling


Meth Meaning
od

isnull( Detect missing


) values

dropn Remove missing


a() values
fillna() Fill missing
values

replac Replace values


e()

ffill Forward fill

bfill Backward fill

🔷 Key Points (Exam Important)


●​ 📌 Missing data = NaN
●​ 📌 isnull() → detects missing values
●​ 📌 dropna() → removes missing data
●​ 📌 fillna() → replaces missing data
●​ 📌 Very important in data cleaning

🔷 Final Idea
👉 Missing Data = incomplete or undefined values in dataset
Pandas provides tools to detect, remove, or fill missing values
efficiently.
🔷 Index Hierarchy in Pandas (MultiIndex)
🔹 What is Index Hierarchy?
Index Hierarchy (also called Multi-Level Indexing or MultiIndex)
means using more than one level of index in a Series or DataFrame.

👉 In simple words:​
Instead of one index, we use multiple indexes (levels) to organize
data.

🔹 Why do we use Index Hierarchy?


●​ 📊 To represent complex data structures
●​ 📂 To group data in multiple levels (like Country → State → City)
●​ ⚡ Easier data analysis
●​ 🔍 Better data organization
●​ 🤖 Used in advanced Data Science

🔷 1. Creating MultiIndex in Series


🔹 Syntax:
[Link](data, index=[level1, level2])

🔹 Example:
import pandas as pd

data = [100, 200, 300, 400]


index = [
["India", "India", "USA", "USA"],
["A", "B", "A", "B"]
]

s = [Link](data, index=index)
print(s)

🔹 Output:
India A 100
B 200
USA A 300
B 400
dtype: int64

🔷 2. Creating MultiIndex in DataFrame


🔹 Example:
data = {
"Marks": [90, 85, 88, 92]
}

index = [Link].from_tuples([
("India", "Delhi"),
("India", "Mumbai"),
("USA", "New York"),
("USA", "Boston")
])

df = [Link](data, index=index)
print(df)

🔹 Output:
Marks
India Delhi 90
Mumbai 85
USA New York 88
Boston 92

🔷 3. Accessing Data in MultiIndex


🔹 Using loc[]
➤ Syntax:
[Link]["level1"]

🔹 Example:
print([Link]["India"])

🔹 Output:
Marks
Delhi 90
Mumbai 85

🔷 4. Accessing Specific Value


➤ Syntax:
[Link][("level1", "level2")]

➤ Example:
print([Link][("USA", "Boston")])

🔹 Output:
Marks 92
Name: (USA, Boston), dtype: int64

🔷 5. Creating MultiIndex using from_product()


➤ Syntax:
[Link].from_product([[list1], [list2]])

➤ Example:
index = [Link].from_product(
[["India", "USA"], ["A", "B"]]
)

print(index)

🔷 6. Key Features of Index Hierarchy


●​ 📌 Multiple levels of indexing
●​ 📌 Better organization of data
●​ 📌 Used for grouped data analysis
●​ 📌 Helps in hierarchical data representation
🔷 7. Real-Life Example
Count State Sal
ry es

India Delhi 100

India Mumb 200


ai

USA New 300


York

USA Boston 400

👉 This is a perfect case for MultiIndex (Index Hierarchy)

🔷 Summary
Concept Meaning

Index Hierarchy Multiple index levels

MultiIndex Pandas feature for hierarchical data

loc[] Used for accessing data

Use Complex structured data

🔷 Final Idea
👉 Index Hierarchy = multiple levels of indexing to organize
complex data efficiently

It is very useful in:

📊 Data analysis
📈 Grouped datasets
●​

🧠 Advanced Pandas operations


●​
●​

You might also like