Before combining datasets, ask: which column identifies each record reliably? A primary key is a column or set of columns whose values uniquely identify records. It provides the reference point for relating DataFrames and checking whether other datasets point to existing records. section 2.5.4
Customers, orders, and payments
In a customer DataFrame, customer identifiers act as primary keys. An orders DataFrame can reference those identifiers, while an order_payments DataFrame can use order_id as a foreign key referencing the primary key in df_orders. Inspect the relationships rather than trusting identical column names: missing references or unexpected one-to-one relationships can reveal data-quality problems. section 2.5.4
Listing 2.2 Identifying foreign key violations
foreign_key_violations =
df_orders[~df_orders['order_id'].isin(df_order_payments['order_id'])]
if not foreign_key_violations.empty:
print("Foreign key violations found:")
print(foreign_key_violations)
# Handle violations (e.g., drop rows or correct the foreign key values)
Use the foreign-key violation check to find records whose supposed references do not exist in the primary-key dataset. section 2.5.4




















