What is NumPy and Pandas?

NumPy and Pandas are Python libraries that do one thing very well: handle data fast. Without them, analyzing 10,000 bank transactions means writing loops. With them, you write one line of code and let the library do the work.

The Problem: Imagine a list of 1 million transaction amounts. You need to apply 7% interest to each. A Python loop would iterate 1 million times. NumPy does the same operation on the entire list at once—much faster.
NumPy/Pandas: vectorized operations = no explicit loops

NumPy gives you arrays (think: super-fast lists). Pandas gives you DataFrames (think: smart spreadsheets). Together they unlock data science in Python.

NumPy arrays: speed through structure

A NumPy array is like a Python list, but every element has the same data type (int, float, bool) and sits in contiguous memory. This tight structure lets NumPy apply math to thousands of numbers at once.

1D Array
A single row of numbers
np.array([10, 20, 30])
2D Array
A table of rows & columns
np.array([[1, 2], [3, 4]])
dtype
The data type: int, float, bool
arr.dtype → int64
Broadcasting
Apply operations across dimensions
arr * 2 → doubles all
🏦
Real-world scenario
SouthBank India: analyzing 10,000 transaction rows

SouthBank India processes payments across Mumbai, Delhi, and Bangalore. Every day, 10,000 transactions flow through their system. The fraud team wants to know:

  • Total transaction volume across all branches
  • Which customers spend the most
  • Average transaction size per month
10,000
Transactions
₹2.3 Cr
Total Volume
3 months
Data range

Building the analysis: code by code

Let's solve the SouthBank India scenario step-by-step, learning NumPy and Pandas as we go.

📊
Download the sample dataset
Save next to your notebook so pd.read_csv('transactions.csv') works.

Step 1: Create and operate on NumPy arrays

Start with NumPy. We have 1 million user IDs and need to calculate bonus amounts (7% of balance). NumPy does this instantly.

code1.py NumPy: vectorized arithmetic
Output
Balances: [ 45000 78500 123000 56700 89200 102000 67800 95400 38900 156000] 7% Bonus: [ 3150. 5495. 8610. 3969. 6244. 7140. 4746. 6678. 2723. 10920.] Total bonus pool: ₹ 59515.0

Step 2: Load CSV and explore with Pandas

Real data lives in CSV files. Pandas loads them into a DataFrame—a labeled, 2D table that feels like Excel but with Python superpowers.

code2.py Pandas: load and explore CSV
Output
date branch amount customer_id 0 2024-01-01 Mumbai 45000 C001 1 2024-01-01 Delhi 67800 C012 2 2024-01-02 Bangalore 123000 C005 3 2024-01-02 Mumbai 78500 C008 4 2024-01-03 Delhi 89200 C003 Data Info: - amount: 10000 entries, dtype int64 - branch: 10000 entries, dtype object Basic Statistics: amount count 10000.00 mean 23000.25 std 35800.12 max 250000.00

Step 3: Filter rows with boolean masks

You want only large transactions (over ₹50,000) to check for fraud. Pandas boolean filtering does this in one line.

code3.py Pandas: filtering with boolean masks
Output
date branch amount customer_id 0 2024-01-01 Mumbai 67800 C001 1 2024-01-02 Bangalore 123000 C005 2 2024-01-03 Delhi 89200 C003 Found 3247 transactions over ₹50,000 Mumbai large txns: 1089

Step 4: Group by and aggregate

Group transactions by branch, then sum the amounts. This tells you which branch processed the most volume.

code4.py Pandas: groupby aggregation
Output
branch Mumbai ₹750,000 Delhi ₹680,000 Bangalore ₹870,000 Transaction count per branch: Mumbai 3500 Delhi 3200 Bangalore 3300 Summary by branch: sum count mean Mumbai 750000 3500 214.29 Delhi 680000 3200 212.50 Bangalore 870000 3300 263.64

Step 5: Sort and save your analysis

Sort by amount, add new columns (like month extracted from date), and save the cleaned data back to CSV.

code5.py Pandas: sorting, adding columns, saving
Output
date branch amount 0 2024-01-15 Bangalore 250000 1 2024-02-20 Mumbai 245000 2 2024-01-10 Delhi 240000 ... Monthly totals: month 1 850000 2 820000 3 830000 Saved to transactions_analyzed.csv

Where you see this

Industries & use cases

🏦
Banking & Fraud Detection
Analyze millions of transactions to flag suspicious patterns, calculate daily volumes, identify top spenders
📊
E-commerce Analytics
Jumia Kenya: calculate average order value per city, find top SKUs, track inventory turnover
🏥
Healthcare Records
Filter patient data by condition, group by hospital, calculate recovery rates
📱
Telecom & Network
Analyze call logs, data usage per user, predict churn by region

How this connects to AI

Every machine learning model starts with Pandas. You load data with pd.read_csv(), clean it with filtering and groupby, feed it into scikit-learn or TensorFlow. Before you can build a fraud detector, you need to understand what fraud looks like—that's NumPy and Pandas. Models are fast; data prep is everything.

vectorized operations data transformation feature engineering

Mini Challenge

Task: You have a CSV with customer orders. Load it, find all orders over ₹100,000, group by customer_id, and sum their total spending. Which customer spent the most? Print the answer and save the result to top_customers.csv.

Hint: Use df.groupby('customer_id')['amount'].sum().sort_values(ascending=False).head(1) to find the top customer.

Interview questions

NumPy arrays store elements contiguously in memory with a fixed data type. Python lists store pointers to objects scattered across memory. NumPy's tight structure lets operations use CPU optimizations (SIMD) that Python can't reach.
.loc selects by label (column name, row index). .iloc selects by position (0, 1, 2...). Use df.loc[df['amount'] > 50000] for label-based filtering; use df.iloc[0:5] to grab rows 0–4 by position.
A Series is a single column of data (like a list with labels). A DataFrame is multiple columns (like a table). Use a Series for 1D operations (e.g., customer names); use a DataFrame when you have multiple related columns (transactions with date, amount, branch).
Use .isna() to find nulls, .dropna() to remove rows with nulls, or .fillna(value) to replace them. Example: df.fillna(0) replaces all NaN with 0, and df.dropna(subset=['amount']) drops rows where amount is null.
Broadcasting lets NumPy apply operations between arrays of different shapes. E.g., arr + 5 adds 5 to every element without looping. More complex: a 3x4 array plus a 4-element array broadcasts the 4-element array across all 3 rows. This is what makes NumPy so fast for vectorized math.

Test your knowledge

Question 1 of 5
What does df[df['amount'] > 50000] do?
Question 2 of 5
Which is faster: for x in arr: x * 2 or arr * 2 in NumPy?
Question 3 of 5
What does df.groupby('branch')['amount'].sum() return?
Question 4 of 5
How do you load a CSV file into a Pandas DataFrame?
Question 5 of 5
Which task is NOT something you can do with Pandas groupby?
0 / 5

Cheatsheet

Task Code Returns
Create 1D array np.array([1, 2, 3]) NumPy array [1, 2, 3]
Create 2D array np.array([[1, 2], [3, 4]]) 2x2 matrix
Vectorized math arr * 1.07 All elements multiplied (no loop)
Load CSV df = pd.read_csv('data.csv') DataFrame from file
First 5 rows df.head() First 5 rows
Select column df['amount'] Series of values
Filter rows df[df['amount'] > 50000] Subset of rows
Group & sum df.groupby('branch')['amount'].sum() Total per group
Sort df.sort_values('amount', ascending=False) Rows ordered by amount
Save CSV df.to_csv('out.csv', index=False) Writes to file

Common mistakes

Forgetting to assign the filtered result
Wrong: df[df['amount'] > 50000] (result discarded)
Right: large = df[df['amount'] > 50000] (save to variable)
Using & instead of 'and' in boolean filters
Wrong: df[df['amount'] > 50000 and df['branch'] == 'Mumbai']
Right: df[(df['amount'] > 50000) & (df['branch'] == 'Mumbai')]
Calling .sum() on a string column
Wrong: df['branch'].sum() (TypeError)
Right: df['amount'].sum() or df['branch'].value_counts()
Forgetting to convert dates with pd.to_datetime()
Wrong: df['date'].dt.month (fails if date is string)
Right: df['date'] = pd.to_datetime(df['date']); df['date'].dt.month

Key takeaways

NumPy arrays: Fast, fixed-type containers for numerical operations. Use vectorization (arr * 2) instead of loops.

Pandas DataFrames: Labeled 2D tables. Load with pd.read_csv(), explore with .head() and .describe().

Boolean filtering: df[df['col'] > value] returns a subset. Combine with & (and) or | (or).

GroupBy & aggregation: df.groupby('column').agg({'col2': 'sum', 'col3': 'mean'}) transforms raw data into insights.

Sorting & saving: .sort_values() orders rows; .to_csv() exports cleaned data.

Data science starts here: Before building any model, you'll spend 80% of time loading, cleaning, and exploring with NumPy & Pandas.

Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

Ready to dive deeper?

Join the Best AI Program: The AI-Era Analyst — 5 months, fully online, with placement assistance. Week 1 is free.

Explore The AI-Era Analyst →