NumPy and pandas are the two foundations of data science in Python. pandas is built on top of NumPy, so they work together – but each is best at different jobs.
What You Will Learn
- The core difference between ndarray and DataFrame
- When to use NumPy and when pandas
- Converting between them
- Speed and memory differences
Prerequisites
Introduction to NumPy and Introduction to Pandas.
Side by Side
import numpy as np, pandas as pd
arr = np.array([[82, 15000], [91, 30000], [68, 15000]])
print(arr[:, 0].mean()) # NumPy: columns by position
df = pd.DataFrame(arr, columns=["marks", "fee"], index=["Aman", "Riya", "Karan"])
print(df["marks"].mean()) # pandas: columns by name
print(df.loc["Riya", "fee"]) # rows by label
print(df.to_numpy().dtype, type(df["marks"].to_numpy()).__name__)
80.33333333333333 80.33333333333333 30000 int64 ndarray
Explanation: The numbers and result are the same, but pandas adds labels: named columns and a row index. .to_numpy() converts back to a NumPy array – which is what scikit-learn and Keras use internally.
Comparison
| NumPy | pandas | |
|---|---|---|
| Main object | ndarray (n-dimensional array) | Series (1-D) and DataFrame (2-D table) |
| Data types | One type per array | Different type per column |
| Labels | Positions only | Column names and row index |
| Missing data | NaN for floats only | NaN/NA handling built in |
| Best for | Maths, matrices, images, ML inputs | Tables, CSV/Excel/SQL, cleaning, grouping, time series |
| Speed | Faster for pure numeric operations | Slight overhead for labels and checks |
| Typical functions | dot, reshape, linalg, random | read_csv, groupby, merge, pivot_table, resample |
Speed
import time
x = np.random.default_rng(0).random(5_000_000)
s = pd.Series(x)
t = time.perf_counter(); x.sum(); a = time.perf_counter() - t
t = time.perf_counter(); s.sum(); b = time.perf_counter() - t
print(a < b)
True
Explanation: For a plain numeric sum, NumPy is faster because pandas also handles the index and missing values. The difference is small for most tasks; choose by convenience, not micro-speed.
When to Use Which
- Load, clean, explore, group and join tabular data → pandas.
- Matrix maths, images, simulations, feeding models → NumPy.
- Typical workflow: read and clean with pandas →
df.to_numpy()or pass the DataFrame to scikit-learn → results back into pandas for reporting.
Series
NumPy series: Introduction · Creating Arrays · Data Types · Slicing & Indexing · Array Manipulation · Broadcasting · Math Functions · Statistical Functions · Random · Linear Algebra · NumPy vs Pandas
Pandas series: Introduction · Series · DataFrame · Selection & Filtering · Missing Data · Apply & Map · DateTime · Merging · GroupBy · Pivot Tables
Interview Questions
Q: Is pandas built on NumPy?
Yes; DataFrame columns are stored as NumPy (or Arrow) arrays.
Q: When would you choose NumPy over pandas?
For numerical computing on homogeneous arrays, like matrices or image data.
Practice Exercises
- Load a CSV with pandas, select numeric columns and convert them to a NumPy array.
- Compute column means with NumPy and pandas and compare results.
Summary
NumPy offers fast homogeneous arrays for maths; pandas adds labelled, mixed-type tables for real-world data work. Use them together.
Next: Pandas apply and map
Machine Learning Tutorials, Courses and Certifications