Lesson 11 · Data analytics zero to hero
Pandas Merge, Join & Pivot Explained | Data Analytics #11
Video eleven of the 30-part series: combining multiple real tables into one, and reshaping between wide and long formats. We're switching datasets: a real…
- CourseData analytics zero to hero
- Lesson11 of 30
- Video12 min
- FormatJupyter notebook · 9 code cells
- Data6 datasets
What you'll learn
Datasets used in this lesson
Save these next to the notebook. In Google Colab, upload them with the 📁 icon on the left first.
- orders_sample.csv205.5 KB
- customers_sample.csv101.0 KB
- order_items_sample.csv176.5 KB
- products_sample.csv91.4 KB
- category_translation.csv2.5 KB
- payments_sample.csv66.1 KB
📓 Full notebook
Download .ipynbData Analytics Zero to Hero, Video 11: Merging, Joining, and Reshaping#
- Video eleven of the 30-part series: combining multiple real tables into one, and reshaping between wide and long formats.
- We're switching datasets: a real extract of the Olist Brazilian E-Commerce dataset, split naturally across separate tables for orders, customers, items, and products, exactly like a real production database.
- Let's jump straight in.
Before You Start#
- Open a new Jupyter Notebook in VS Code and select your Python interpreter as the kernel.
- Place orders_sample.csv, customers_sample.csv, order_items_sample.csv, products_sample.csv, category_translation.csv, and payments_sample.csv in the same folder as this notebook.
Part 1: Loading Multiple Real Tables#
import pandas as pd
orders = pd.read_csv('orders_sample.csv')
customers = pd.read_csv('customers_sample.csv')
items = pd.read_csv('order_items_sample.csv')
products = pd.read_csv('products_sample.csv')
print(orders.shape, customers.shape, items.shape, products.shape)
print(orders[['order_id', 'customer_id', 'order_status']].head(3))
print(items[['order_id', 'product_id', 'price']].head(3))
Part 2: merge and Inner Joins#
orders_customers = orders.merge(customers, on='customer_id', how='inner')
print(orders_customers.shape)
print(orders_customers[['order_id', 'customer_id', 'customer_state']].head(3))
Part 3: Left, Right, and Outer Joins#
order_items_full = items.merge(products, on='product_id', how='left')
print(order_items_full.shape)
print(order_items_full['product_category_name'].isna().sum())
translation = pd.read_csv('category_translation.csv')
print(translation.head(3))
order_items_full = order_items_full.merge(translation, on='product_category_name', how='left')
print(order_items_full[['product_category_name', 'product_category_name_english']].drop_duplicates().head(5))
Part 4: Building One Combined Table#
payments = pd.read_csv('payments_sample.csv')
full = (orders
.merge(customers, on='customer_id', how='left')
.merge(items, on='order_id', how='left')
.merge(products, on='product_id', how='left')
.merge(payments, on='order_id', how='left'))
print(full.shape)
print(full[['order_id', 'customer_state', 'price', 'payment_type']].head(5))
Part 5: concat#
delivered = orders[orders['order_status'] == 'delivered'].head(3)
shipped = orders[orders['order_status'] == 'shipped'].head(3)
combined = pd.concat([delivered, shipped])
print(combined[['order_id', 'order_status']])
Part 6: Reshaping melt and pivot#
state_totals = full.groupby('customer_state')['price'].sum().reset_index().head(6)
print(state_totals)
long_form = state_totals.melt(id_vars='customer_state', var_name='metric', value_name='value')
print(long_form)
Wrap-Up: What You Learned#
- merge for combining two tables on a shared key, and the four join types: inner, left, right, and outer.
- Chaining multiple merges together to build one wide table from several real source tables.
- concat for stacking DataFrames by position rather than by key.
- melt for reshaping wide data into long, tidy rows.
- All of it run against a real, multi-table e-commerce dataset, exactly the shape real production data usually comes in.
- Video twelve covers time series analysis with pandas: resampling, rolling windows, and real date-based trends. Subscribe so it lands automatically see you there.
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



