📓 Study Notes – NumPy & Pandas
Python – Coding & Web Development
1️⃣ Simple Explanation
| Library | Purpose | Key Idea |
|---|---|---|
| NumPy | Handles large, multi‑dimensional numerical arrays & matrices efficiently. | Vectorised operations → no explicit loops. |
| Pandas | Provides high‑level data structures (Series, DataFrame) for labelled & heterogeneous data. |
Built on NumPy; adds indexing, group‑by, I/O, time‑series support. |
Why use them?
- NumPy gives speed for raw calculations.
- Pandas gives convenience for real‑world data analysis (CSV, SQL, etc.).
2️⃣ Key Points
2.1 NumPy
| Feature | What It Is | Why It Matters |
|---|---|---|
np.array |
N‑dimensional container | Core of NumPy; stores homogeneous data. |
dtype |
Data type of array elements | Controls memory usage & precision. |
| Broadcasting | Implicit expansion of arrays | Lets you add a vector to a matrix without loops. |
Universal Functions (ufuncs) |
Element‑wise math | e.g., np.sin, np.exp, np.sqrt. |
| Linear Algebra | np.linalg |
Solve systems, eigenvalues, SVD, etc. |
| Random module | np.random |
Generate synthetic data. |
2.2 Pandas
| Feature | What It Is | Why It Matters |
|---|---|---|
Series |
1‑D labelled array | Handles time‑series, single column data. |
DataFrame |
2‑D labelled table | Core data structure for tabular data. |
Indexing (loc, iloc, []) |
Access by label or integer | Flexible selection. |
groupby |
Aggregate by key(s) | Summarise, pivot, transform. |
merge, concat, join |
Relational operations | Combine multiple tables. |
Missing data handling (NaN) |
fillna, dropna |
Clean & impute. |
I/O (read_csv, to_excel, etc.) |
Import/Export | Connect to databases, files. |
| Time‑Series tools | pd.date_range, resampling |
Finance, sensor data. |
3️⃣ Worked Example
We'll walk through a mini‑project: sales data analysis.
3.1 Import Libraries
import numpy as np
import pandas as pd
3.2 Create a NumPy array
# 3 rows, 4 columns – sales in units
sales = np.array([[10, 12, 9, 11],
[7, 14, 8, 9],
[12, 13, 11, 15]])
print(sales)
Output
[[10 12 9 11]
[ 7 14 8 9]
[12 13 11 15]]
3.3 Basic NumPy ops
# Total sales per product (column sum)
total_per_product = sales.sum(axis=0) # axis=0 => sum rows
# Average daily sales
avg_daily = sales.mean(axis=1) # axis=1 => sum columns
print("Total per product:", total_per_product)
print("Avg daily sales:", avg_daily)
Output
Total per product: [29 39 28 35]
Avg daily sales: [10.5 10. 13. ]
3.4 Convert to Pandas DataFrame
# Column names: P1, P2, P3, P4
df = pd.DataFrame(sales,
columns=['P1', 'P2', 'P3', 'P4'],
index=['Day1', 'Day2', 'Day3'])
print(df)
P1 P2 P3 P4
Day1 10 12 9 11
Day2 7 14 8 9
Day3 12 13 11 15
3.5 Add computed columns
df['Total'] = df.sum(axis=1)
df['Avg'] = df.mean(axis=1)
print(df)
P1 P2 P3 P4 Total Avg
Day1 10 12 9 11 42 10.5
Day2 7 14 8 9 38 10.0
Day3 12 13 11 15 51 13.0
3.6 Grouping & Aggregation
Suppose we add a Region column.
df['Region'] = ['East', 'West', 'East']
print(df)
P1 P2 P3 P4 Total Avg Region
Day1 10 12 9 11 42 10.5 East
Day2 7 14 8 9 38 10.0 West
Day3 12 13 11 15 51 13.0 East
Group by region and compute mean sales per product.
grouped = df.groupby('Region')[['P1', 'P2', 'P3', 'P4']].mean()
print(grouped)
P1 P2 P3 P4
Region
East 11.0 12.5 10.0 13.0
West 7.0 14.0 8.0 9.0
3.7 Merging DataFrames
Create a second DataFrame of Discounts per region.
discounts = pd.DataFrame({
'Region': ['East', 'West'],
'Disc%': [5, 10]
}).set_index('Region')
merged = df.join(discounts, on='Region')
print(merged)