A dimension table stores descriptive attributes that add context to records in other tables. Product category, person location, and birth date are typical examples. section 2.3.1
Use dimension tables carefully when assembling analytical datasets:
- Join them to event or item tables to enrich transactions, visits, or other tracked activities. section 2.3.1
- Check whether descriptive values preserve change history or overwrite earlier values. section 2.3.1
- Treat overwritten or temporally misaligned values as possible noise or future-information leakage in predictive models. section 2.3.1
Product enrichment
A product transaction table can be combined with dimension data to add descriptions, categories, manufacturers, and weights for analysis. section 2.3.1
Listing 2.4 Merging datasets in pandas
import pandas as pd
df1 = pd.DataFrame({'product_id': [1, 2, 3, 4],
'product_name': ['Product A',
'Product B',
'Product C',
'Product D'],
'price': [10.99, 20.99, 15.99, 8.99]}) #1
df2 = pd.DataFrame({'product_id': [1, 2, 3, 4],
'product_description': ['A great product',
'A high-quality product',
'A reliable product',
'An affordable product'],
'category': ['Category A', 'Category B',
'Category C', 'Category D']}) #2
df3 = pd.DataFrame({'product_id': [1, 2, 3, 4],
'manufacturer': ['Manufacturer A', 'Manufacturer B',
'Manufacturer C', 'Manufacturer D'],
'weight': [1.5, 2.0, 1.8, 1.2]}) #3
merged_df = pd.merge(df1, df2, on='product_id') #4
merged_df = pd.merge(merged_df, df3, on='product_id') #5
print(merged_df)
A pandas merge example demonstrates assembling product tables through a shared product identifier. section 2.3.1
