Python is the dominant language for data science thanks to three core libraries: NumPy for numerical computation, Pandas for data manipulation, and Matplotlib for visualization. Together they form the foundation of nearly every data workflow.
The typical data science workflow is: load data, clean and transform it, explore patterns with statistics and plots, build models, and communicate results. Jupyter notebooks provide an interactive environment for each step.
NumPy Arrays
NumPy provides the ndarray, a fast, fixed-type multidimensional array. Vectorized operations on NumPy arrays are 10-100x faster than equivalent Python loops because they run in compiled C code.
numpy_basics.py
Python
1
import numpy as np
2
3
# Creating arrays
4
a = np.array([1, 2, 3, 4, 5]) # from list
5
b = np.zeros((3, 4)) # 3x4 zeros
6
c = np.ones((2, 3, 4)) # 3D ones
7
d = np.arange(0, 10, 0.5) # like range() but float
8
e = np.linspace(0, 1, 100) # 100 evenly spaced [0,1]
9
f = np.random.randn(3, 4) # 3x4 normal distribution
# Broadcasting — operations between different shapes
18
matrix = np.arange(12).reshape(3, 4)
19
row = np.array([1, 0, 1, 0])
20
print(matrix + row) # adds row to each row of matrix
21
22
# Boolean indexing
23
data = np.array([1, 5, 3, 8, 2, 9, 4])
24
mask = data > 4
25
print(data[mask]) # [5 8 9]
26
print(np.sum(mask)) # 3 elements > 4
27
28
# Linear algebra
29
A = np.array([[1, 2], [3, 4]])
30
B = np.array([[5, 6], [7, 8]])
31
print(A @ B) # matrix multiply
32
print(np.linalg.inv(A)) # inverse
33
print(np.linalg.det(A)) # determinant
ℹ
info
Always prefer vectorized NumPy operations over Python loops. If you find yourself writing a for-loop over array elements, there is almost certainly a NumPy function that does it faster.
Pandas DataFrames
Pandas builds on NumPy to provide labeled, heterogeneous tabular data via DataFrame and Series. It is the primary tool for data cleaning, transformation, and exploration.
pandas_basics.py
Python
1
import pandas as pd
2
3
# Creating DataFrames
4
df = pd.DataFrame({
5
"name": ["Alice", "Bob", "Charlie", "Diana"],
6
"age": [28, 35, 42, 31],
7
"city": ["NYC", "SF", "NYC", "LA"],
8
"salary": [85000, 120000, 95000, 110000],
9
})
10
11
# From CSV
12
# df = pd.read_csv("data.csv")
13
# From JSON
14
# df = pd.read_json("data.json")
15
# From Excel
16
# df = pd.read_excel("data.xlsx")
17
18
# Basic inspection
19
print(df.shape) # (4, 4)
20
print(df.head(2)) # first 2 rows
21
print(df.dtypes) # column types
22
print(df.describe()) # summary statistics
23
print(df.info()) # non-null counts and types
pandas_select.py
Python
1
# Selection and filtering
2
print(df["name"]) # Series
3
print(df[["name", "age"]]) # DataFrame subset
4
5
# loc — label-based
6
print(df.loc[0:2, "name":"city"]) # rows 0-2, name through city
7
8
# iloc — integer position
9
print(df.iloc[0:2, 0:3]) # first 2 rows, first 3 columns
Always inspect your data before cleaning. Use df.info(), df.describe(), and df.isnull().sum() to understand what you are working with before making changes.
In Jupyter notebooks, add %matplotlib inline at the top to display plots directly in the notebook. Use %timeit to benchmark code and %load_ext autoreload to auto-reload changed modules.