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.
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.
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
Building the analysis: code by code
Let's solve the SouthBank India scenario step-by-step, learning NumPy and Pandas as we go.
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.
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.
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.
Step 4: Group by and aggregate
Group transactions by branch, then sum the amounts. This tells you which branch processed the most volume.
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.
Where you see this
Industries & use cases
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.
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.
df.groupby('customer_id')['amount'].sum().sort_values(ascending=False).head(1) to find the top customer.
Interview questions
.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.
.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.
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
df[df['amount'] > 50000] do?for x in arr: x * 2 or arr * 2 in NumPy?df.groupby('branch')['amount'].sum() return?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
df[df['amount'] > 50000] (result discarded)Right:
large = df[df['amount'] > 50000] (save to variable)df[df['amount'] > 50000 and df['branch'] == 'Mumbai']Right:
df[(df['amount'] > 50000) & (df['branch'] == 'Mumbai')]df['branch'].sum() (TypeError)Right:
df['amount'].sum() or df['branch'].value_counts()df['date'].dt.month (fails if date is string)Right:
df['date'] = pd.to_datetime(df['date']); df['date'].dt.monthKey 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.
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 →