Module 5 App
Module 5 App
🔹 1. Creating a 1D Array
➤ Syntax:
[Link]([elements])
➤ Example:
import numpy as np
➤ 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)
➤ Example:
arr = [Link](1, 10, 2)
print(arr)
➤ 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)
🔹 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]
➤ Example:
import numpy as np
🔷 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])
➤ Example:
a = [Link]([1, 2, 3])
b = [Link]([4, 5, 6])
print(a + b)
print(a * b)
👉 Step-by-step:
● Multiply each element by 2 → [4, 8, 12]
● Add 3 → [7, 11, 15]
🔹 1. Indexing in 1D Array
➤ Syntax:
array[index]
➤ Example:
import numpy as np
🔹 2. Negative Indexing
👉 Used to access elements from the end
➤ Syntax:
array[-index]
➤ Example:
arr = [Link]([10, 20, 30, 40])
or
array[row_index, column_index]
➤ Example:
arr = [Link]([[1, 2, 3],
[4, 5, 6]])
🔹 4. Indexing in 3D Array
➤ Syntax:
array[block][row][column]
➤ Example:
arr = [Link]([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
🔹 5. Slicing in 1D Array
➤ Syntax:
array[start:stop:step]
➤ Example:
arr = [Link]([10, 20, 30, 40, 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]]
🔷 Quick Summary
Concept Syntax
1D indexing array[i]
1D slicing array[start:stop]
👉 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
print(arr.T)
➤ Output:
[1 2 3 4]
➤ 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]]]
● array.T
● [Link](array)
✔ 1D array does not change
🔷 Quick Summary
Method Syntax Purpose
● arithmetic operations
● mathematical functions
● transformations
● aggregation (sum, mean, etc.)
● element-wise processing
➤ Example:
import numpy as np
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]]
➤ Example:
arr = [Link]([10, 15, 20, 25, 30])
➤ Output:
[25 30]
➤ Output:
[6 7 8]
🔷 Summary Table
Type Example
Arithmetic arr + 5
Array ops a + b
Math [Link]()
Aggregation [Link]()
Reshape reshape()
🔹 Syntax:
[Link](list(input().split()))
🔹 Example:
import numpy as np
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
print(arr)
🔹 Input:
Enter rows: 2
123
456
🔹 Output:
[[1 2 3]
[4 5 6]]
➤ Simple Output
🔹 Syntax:
print(array)
🔹 Example:
import numpy as np
➤ 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])
🔹 Example:
arr = [Link]([10, 20, 30])
[Link]("[Link]", arr)
🔹 4. Loading Saved Array
🔹 Syntax:
[Link]("[Link]")
🔹 Example:
data = [Link]("[Link]")
print(data)
🔷 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.
👉 In simple words:
Pandas helps you work with Excel-like data in Python.
🔹 Installation of Pandas
You can install Pandas using pip:
🔹 Importing Pandas
import pandas as pd
➤ Syntax:
[Link](data)
➤ Example:
import pandas as pd
➤ 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
📊 Data Science
🤖 Machine Learning
●
📈 Financial Analysis
●
🔹 Applications of Pandas
1. Data Analysis
2. Data Cleaning
Removes missing or incorrect data.
4. File Handling
● CSV
● Excel
● JSON
🔷 Summary
Concept Meaning
👉 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
🔹 Syntax of Series
[Link](data, index)
🔹 Creating a Series
🔸 1. From List
import pandas as pd
🔹 Output:
0 10
1 20
2 30
3 40
dtype: int64
🔹 Output:
a 100
b 200
c 300
dtype: int64
s = [Link](data)
print(s)
🔹 Output:
Math 90
Science 85
English 88
dtype: int64
print(s[0])
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))
🔷 Summary
Featur Description
e
👉 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.
🔹 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
data = {
"Name": ["Asha", "Rahul"],
"Age": [20, 21]
}
🔹 Output:
Name Age
A Asha 20
B Rahul 21
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])
👉 Example:
print(df["Name"])
🔸 2. Multiple Columns
df[["Name", "Age"]]
🔸 4. Column Names
[Link]
🔸 5. Summary
[Link]()
🔹 Features of DataFrame
● 📊 2D tabular structure
● 📌 Rows and columns
● ⚡ Fast data processing
● 🧹 Easy data cleaning
● 🔍 Powerful indexing
🔷 Summary
Feature Description
DataFra 2D table in
me Pandas
Structure Rows +
Columns
Column Series
type
🔹 Syntax
[Link]
[Link]
print([Link])
🔹 Output:
Index(['a', 'b', 'c'], dtype='object')
df = [Link](data)
print([Link])
🔹 Output:
RangeIndex(start=0, stop=2, step=1)
🔸 2. Custom Index
We can define our own index.
🔸 3. RangeIndex
Used when index is numeric sequence.
➤ 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()
➤ Example:
df.set_index("Name")
🔷 Summary
Concept Meaning
🔷 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
🔹 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
🔹 Output:
a 10.0
b 20.0
c 30.0
d NaN
dtype: float64
data = {
"Name": ["Asha", "Rahul", "Kiran"],
"Age": [20, 21, 22]
}
🔹 Output:
Name Age
a Asha 20.0
b Rahul 21.0
c Kiran 22.0
d NaN NaN
🔹 Output:
Age Name Marks
a 20.0 Asha NaN
b 21.0 Rahul NaN
c 22.0 Kiran NaN
🔸 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
🔷 Summary
Feature Description
🔹 Syntax of Drop
➤ For Series / DataFrame (rows or columns):
[Link](labels, axis)
🔹 Meaning of Parameters
Parame Meaning
ter
➤ Example:
import pandas as pd
data = {
"Name": ["Asha", "Rahul", "Kiran"],
"Age": [20, 21, 22]
}
df = [Link](data)
🔹 Output:
Name Age
0 Asha 20
2 Kiran 22
🔹 Output:
Name Age
1 Rahul 21
➤ Example:
df2 = [Link]("Age", axis=1)
print(df2)
🔹 Output:
Name
0 Asha
1 Rahul
2 Kiran
➤ Example:
s = [Link]([10, 20, 30], index=["a", "b", "c"])
s2 = [Link]("b")
print(s2)
🔹 Output:
a 10
c 30
dtype: int64
➤ Example:
[Link](1, inplace=True)
print(df)
🔹 Key Points
● 📌 axis=0 → rows
● 📌 axis=1 → columns
● 📌 drop() removes data
● 📌 inplace=True modifies original data
● 📌 Used for data cleaning
🔷 Summary
Operation Syntax
🔷 Final Idea
👉 Drop entry = removing unwanted rows or columns from
Pandas data
➤ Example:
import pandas as pd
print(s["b"])
🔹 Output:
20
➤ Example:
print(s[1])
🔹 Output:
20
➤ Example:
print(s[0:2])
🔹 Output:
a 10
b 20
dtype: int64
➤ 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
➤ Example:
print(df[["Name", "Age"]])
➤ Example:
df = [Link](data, index=["a", "b", "c"])
print([Link]["b"])
🔹 Output:
Name Rahul
Age 21
Name: b, dtype: object
➤ Example:
print([Link][1])
🔹 Output:
Name Rahul
Age 21
Name: b, dtype: object
➤ Example:
print([Link]["b", "Name"])
🔹 Output:
Rahul
➤ Example:
print(df[df["Age"] > 20])
🔹 Output:
Name Age
b Rahul 21
c Kiran 22
[] Select column
loc[ Label-based
] selection
iloc Position-based
[] selection
Boole Condition-based
an filtering
🔷 Final Idea
👉 Select Entries = extracting specific rows, columns, or values
from Pandas data
📊 Data analysis
🧹 Data cleaning
●
👉 In simple words:
Pandas aligns data using index before doing calculations.
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
🔹 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:
print(df1 + df2)
🔹 Output:
A B
x NaN NaN
y NaN NaN
👉 Because columns are different → no match → NaN
● ✔ Aligns indexes
● ✔ Aligns columns
● ✔ Handles missing data
● ✔ Avoids manual mapping
🔷 Summary
Concept Meaning
🔷 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:
🔹 Syntax of Sorting
➤ Series:
series.sort_values()
➤ DataFrame:
df.sort_values(by="column_name")
🔹 1. Sorting a Series
➤ Example:
import pandas as pd
print(s.sort_values())
🔹 Output:
1 10
3 20
2 30
0 40
dtype: int64
print(df.sort_values(by="Age"))
🔹 Output:
Name Age
1 Rahul 20
2 Kiran 21
0 Asha 22
➤ 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.
print([Link]())
🔹 Output:
0 4.0
1 2.0
2 3.0
3 1.0
dtype: float64
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
● ascending=True/False
● by="column_name"
Ranking:
● ascending=True/False
● method="average|min|max|first"
🔷 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
➤ Example:
import pandas as pd
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())
➤ Example:
print(df["Marks"].min())
print(df["Marks"].max())
🔹 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
count number of
values
mean average
std standard
deviation
min minimum
value
50% median
75% third quartile
max maximum
value
🔷 5. Variance
👉 Measures how data is spread from mean
➤ Syntax:
[Link]()
mean() Average
sum() Total
var() Variance
🔷 Final Idea
👉 Summary Statistics = mathematical summary of dataset
It helps to:
➤ 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())
➤ Example:
print([Link]())
🔹 Output:
Name Age
0 Asha 20.0
➤ Example:
print([Link](0))
🔹 Output:
Name Age
0 Asha 20.0
1 Rahul 0.0
2 0 22.0
➤ Example:
[Link]([Link], 50)
➤ Example:
[Link](method="ffill")
🔷 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.
🔹 Example:
import pandas as pd
s = [Link](data, index=index)
print(s)
🔹 Output:
India A 100
B 200
USA A 300
B 400
dtype: int64
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
🔹 Example:
print([Link]["India"])
🔹 Output:
Marks
Delhi 90
Mumbai 85
➤ Example:
print([Link][("USA", "Boston")])
🔹 Output:
Marks 92
Name: (USA, Boston), dtype: int64
➤ Example:
index = [Link].from_product(
[["India", "USA"], ["A", "B"]]
)
print(index)
🔷 Summary
Concept Meaning
🔷 Final Idea
👉 Index Hierarchy = multiple levels of indexing to organize
complex data efficiently
📊 Data analysis
📈 Grouped datasets
●