Python Tutorial 0/52 lessons ~6 min read Lesson 48
NumPy & Pandas — Deep Dive
NumPy gives you fast, typed n-dimensional arrays.
Course progress0%
Focus
9 guided sections
Practice signal
Examples included
Career prep
Interview Q&A included
Introduction
NumPy gives you fast, typed n-dimensional arrays. Pandas builds DataFrames on top — labeled rows/columns with SQL-like operations.
Understanding the topic
Core concepts to understand:
- ndarray = fixed dtype, contiguous memory.
- Broadcasting = implicit shape alignment.
- Pandas:
read_csv,groupby,merge,pivot. - Index alignment is automatic in Pandas ops.
Syntax reference
Visual flow / code:
python
import numpy as npimport pandas as pd# NumPyx = np.arange(12).reshape(3, 4)x.mean(axis=0) # column meansx[x > 5] # boolean mask# Pandasdf = pd.DataFrame({"country": ["US","DE","US","DE"],"sales": [100, 80, 120, 90],})df.groupby("country").sum()df.pivot_table(values="sales", index="country", aggfunc="mean")df.merge(other_df, on="country", how="left")
Execution workflow
1NumPy & Pandas — Deep Dive Workflow
1 / 4ndarray = fixed dtype, contiguous memory
ndarray = fixed dtype, contiguous memory.
Real-world use
Pandas powers most analytics, dashboards, and ML feature engineering in Python. For data > RAM, use Polars or DuckDB.
Best practices
- Use vectorized ops, not row loops.
- Set proper dtypes (
category,int32) to save memory. - Chain ops with
.pipe()for readability.
Common mistakes
SettingWithCopyWarning— use.locor.copy().
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1IntermediateQuestionBroadcasting — explain.+
Answer
Broadcasting is NumPy's rule for combining arrays of different shapes. Shapes are aligned from the trailing axis, missing leading axes are treated as size 1, and each pair of dimensions must be equal or contain a 1, which is then repeated virtually. For instance, subtracting a per-column mean of shape
(n,) from a (m, n) matrix works. No actual copy of the expanded data is made.Follow-up
How do you use
np.newaxis to control broadcasting?2AdvancedQuestiongroupby internals?+
Answer
pandas
groupby follows split-apply-combine. It maps each key to integer group codes (factorizing the keys), then uses those codes to partition rows, run an aggregation per group, often in optimized Cython code for built-ins like sum or mean, and assemble the results. Grouping is lazy: the GroupBy object does little until you aggregate. Custom Python functions with apply are much slower than built-ins.Follow-up
What is the difference between
agg, transform, and apply?3BeginnerQuestionPandas vs SQL?+
Answer
Both can filter, join, and aggregate tabular data. SQL runs inside the database near the data, handles data larger than memory, and benefits from indexes and a query planner. Pandas runs in memory in your Python process and makes it easy to do exploratory work, custom transformations, and plotting or modelling. A common approach is to filter and aggregate in SQL, then load the reduced result into pandas.
Follow-up
Which pandas operation corresponds to
GROUP BY ... HAVING?Summary
In summary: NumPy = math; Pandas = tables. Vectorize and dtype-tune for speed.
Ready to mark this lesson complete?Track your journey across the entire course.