iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Intro

NumPy is Pythons array engine. Pandas is the dataframe layer on top. Together they are the foundation of nearly every data tool in Python.

NumPy + Pandas — what they are

EXAMPLE
# ===== The values =====
# NumPy: typed n-dimensional arrays + vectorised math
# Pandas: labelled tables (DataFrame) + time series + groupby + I/O

import numpy as np
import pandas as pd

# ===== NumPy basics =====
a = np.array([1, 2, 3, 4])
b = np.arange(0, 10, 2)
m = np.zeros((3, 4))
print(a.shape, a.dtype)
print(a + 10)        # vectorised
print(a.mean(), a.std())

# Broadcasting:
x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([10, 20, 30])
print(x + y)

# ===== Pandas basics =====
df = pd.DataFrame({
    'name': ['Alex', 'Sam', 'Lee'],
    'age':  [30, 25, 40],
    'city': ['Sydney', 'Sydney', 'Melbourne'],
})
print(df)

print(df[df.age > 28])
print(df.groupby('city').age.mean())

df['decade'] = (df.age // 10) * 10
print(df.sort_values('age', ascending=False))

# ===== I/O =====
df = pd.read_csv('data.csv', parse_dates=['date'])
df.to_parquet('data.parquet')

# ===== When they win =====
# - Any tabular or numeric data work in Python
# - Foundation for scikit-learn, statsmodels, matplotlib, seaborn
# - Notebooks + dataframes are the lingua franca of analysis

# ===== When they hurt =====
# - Multi-GB data on one machine (use Polars, Dask, DuckDB, Spark)
# - Heavy SQL-style work (DuckDB on parquet often beats Pandas now)

# ===== Patterns to internalise =====
# - Vectorise: no Python for loops over rows
# - Specify dtypes on read; lets Pandas skip inference
# - Chain operations; avoid intermediate variables that hide intent
# - Use parquet over CSV for repeated reads

# ===== Pitfalls =====
# - SettingWithCopyWarning: chain into .loc or use copy()
# - apply with a Python function on a hot column -> 10-100x slower than vectorised
# - Memory blowups on large groupby + agg; check chunk size
# - Inconsistent NaN handling (np.nan, pd.NA, None) across versions

Why it matters

NumPy and Pandas are the bedrock of Python data work. Arrays + dataframes + vectorisation + groupby covers most analysis you will ever do. Start by reading, filtering, grouping, and plotting; once those are reflex, the rest of the Python data ecosystem unlocks.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# NumPy: fast n-dimensional arrays + linear algebra.
# Pandas: tabular data (Series + DataFrame) built on NumPy.
Try it Yourself »

Exercise

Canonical NumPy alias.

import numpy as

Test yourself

Q1. NumPy provides…
Q2. Pandas is built on top of…
Q3. A 2D table in Pandas is called a…

Discussion

Loading…