Chapter 2 · Data Scientist
Python for data science
~8 min read
Python is the default language of data science, and two libraries do most of the day-to-day work: pandas for working with tabular data, and NumPy for fast numerical arrays underneath it. You do not need to be a software engineer. You need to be fluent at loading data, shaping it, and computing on it.
2.1 The mental model: DataFrames#
A pandas DataFrame is a table: rows and named columns, like a spreadsheet you control with code. Almost every analysis is some combination of selecting rows and columns, filtering, grouping, and aggregating. If you know SQL, the concepts map over directly, which is why we teach them together.
import pandas as pd
df = pd.read_csv('orders.csv')
df.head() # first rows
df.info() # columns, types, null counts
df.describe() # summary statistics
# Filter, then group and aggregate
paid = df[df['status'] == 'paid']
by_region = paid.groupby('region')['amount'].sum()2.2 The operations you will use constantly#
| Task | pandas | Note |
|---|---|---|
| Select columns | df[['a','b']] | Double brackets for multiple |
| Filter rows | df[df['x'] > 5] | Boolean masks |
| Group and aggregate | df.groupby('k').mean() | The workhorse |
| Join tables | df.merge(other, on='id') | Like a SQL JOIN |
| Handle missing | df.fillna(0) / dropna() | Decide per column |
| New column | df['c'] = df['a'] * 2 | Vectorized, not loops |
2.3 NumPy underneath#
NumPy provides the fast array that pandas is built on. You will use it directly for numerical work: creating arrays, elementwise math, and the statistical functions that power everything from feature scaling to model math. You do not need its full depth now, but be comfortable with arrays, mean, std, and elementwise operations.
2.4 Practice#
You have a DataFrame of transactions. How would you find total revenue per customer?
Why prefer a vectorized operation over a Python loop in pandas?
Get the next chapter and weekly interview tips by email
One short email per week. Skim in a minute. Unsubscribe anytime.
