duplicate_rows = df.loc[df.duplicated("id", keep=False)]
duplicate_rows.sort_values("id")14 Database Workflows
14.1 A simple loading workflow
- Read raw data.
- Preserve identifiers as text.
- Clean missing values.
- Validate columns and row counts.
- Load into a staging table.
- Run database constraints and checks.
- Promote validated data to the target table.
14.2 Validate duplicate keys in Python
14.3 Validate duplicate keys in SQL
SELECT
id,
COUNT(*) AS occurrences
FROM staging.dataset
GROUP BY id
HAVING COUNT(*) > 1;14.4 Transaction pattern
with engine.begin() as connection:
connection.execute(text("TRUNCATE TABLE staging.dataset"))
# Load data here.14.5 Things to remember
- Validation is part of loading, not a separate optional task.
- Record the source, date, and transformation version.
- Prefer repeatable scripts over manual database changes.