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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {},
|
| 6 |
+
"source": [
|
| 7 |
+
"# Phase 5 — Nutrition Integration Audit\n",
|
| 8 |
+
"\n",
|
| 9 |
+
"This notebook audits the authoritative nutrition dataset before implementing Phase 5.\n",
|
| 10 |
+
"\n",
|
| 11 |
+
"**Data sources:**\n",
|
| 12 |
+
"- `saraNour/compliments-brand/source_of_truth/nutrition.parquet`\n",
|
| 13 |
+
"- `saraNour/compliments-brand/source_of_truth/products.parquet`\n",
|
| 14 |
+
"\n",
|
| 15 |
+
"**Requirements:** `pandas`, `pyarrow`, `huggingface_hub`"
|
| 16 |
+
]
|
| 17 |
+
},
|
| 18 |
+
{
|
| 19 |
+
"cell_type": "code",
|
| 20 |
+
"execution_count": 1,
|
| 21 |
+
"metadata": {
|
| 22 |
+
"execution": {
|
| 23 |
+
"iopub.execute_input": "2026-07-30T18:23:30.167225Z",
|
| 24 |
+
"iopub.status.busy": "2026-07-30T18:23:30.166713Z",
|
| 25 |
+
"iopub.status.idle": "2026-07-30T18:23:33.350641Z",
|
| 26 |
+
"shell.execute_reply": "2026-07-30T18:23:33.345093Z"
|
| 27 |
+
}
|
| 28 |
+
},
|
| 29 |
+
"outputs": [
|
| 30 |
+
{
|
| 31 |
+
"name": "stdout",
|
| 32 |
+
"output_type": "stream",
|
| 33 |
+
"text": [
|
| 34 |
+
"Setup complete.\n"
|
| 35 |
+
]
|
| 36 |
+
}
|
| 37 |
+
],
|
| 38 |
+
"source": [
|
| 39 |
+
"!pip install -q pandas pyarrow huggingface_hub\n",
|
| 40 |
+
"\n",
|
| 41 |
+
"import pandas as pd\n",
|
| 42 |
+
"import numpy as np\n",
|
| 43 |
+
"from huggingface_hub import hf_hub_download\n",
|
| 44 |
+
"\n",
|
| 45 |
+
"def load_hf(repo, filename):\n",
|
| 46 |
+
" path = hf_hub_download(repo_id=repo, filename=filename, repo_type='dataset')\n",
|
| 47 |
+
" print(f'Loaded {filename} from {repo}')\n",
|
| 48 |
+
" if filename.endswith('.parquet'):\n",
|
| 49 |
+
" return pd.read_parquet(path)\n",
|
| 50 |
+
" return pd.read_csv(path)\n",
|
| 51 |
+
"\n",
|
| 52 |
+
"print('Setup complete.')"
|
| 53 |
+
]
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"cell_type": "markdown",
|
| 57 |
+
"metadata": {},
|
| 58 |
+
"source": [
|
| 59 |
+
"## 1. Load Data"
|
| 60 |
+
]
|
| 61 |
+
},
|
| 62 |
+
{
|
| 63 |
+
"cell_type": "code",
|
| 64 |
+
"execution_count": 2,
|
| 65 |
+
"metadata": {
|
| 66 |
+
"execution": {
|
| 67 |
+
"iopub.execute_input": "2026-07-30T18:23:33.441813Z",
|
| 68 |
+
"iopub.status.busy": "2026-07-30T18:23:33.441017Z",
|
| 69 |
+
"iopub.status.idle": "2026-07-30T18:23:34.396067Z",
|
| 70 |
+
"shell.execute_reply": "2026-07-30T18:23:34.384222Z"
|
| 71 |
+
}
|
| 72 |
+
},
|
| 73 |
+
"outputs": [
|
| 74 |
+
{
|
| 75 |
+
"name": "stdout",
|
| 76 |
+
"output_type": "stream",
|
| 77 |
+
"text": [
|
| 78 |
+
"Loaded source_of_truth/nutrition.parquet from saraNour/compliments-brand\n"
|
| 79 |
+
]
|
| 80 |
+
},
|
| 81 |
+
{
|
| 82 |
+
"name": "stdout",
|
| 83 |
+
"output_type": "stream",
|
| 84 |
+
"text": [
|
| 85 |
+
"Loaded source_of_truth/products.parquet from saraNour/compliments-brand\n",
|
| 86 |
+
"\n",
|
| 87 |
+
"nutrition: 4440 rows, 21 cols\n",
|
| 88 |
+
"products: 4440 rows, 16 cols\n"
|
| 89 |
+
]
|
| 90 |
+
}
|
| 91 |
+
],
|
| 92 |
+
"source": [
|
| 93 |
+
"nutrition = load_hf('saraNour/compliments-brand', 'source_of_truth/nutrition.parquet')\n",
|
| 94 |
+
"products = load_hf('saraNour/compliments-brand', 'source_of_truth/products.parquet')\n",
|
| 95 |
+
"\n",
|
| 96 |
+
"print(f'\\nnutrition: {len(nutrition)} rows, {len(nutrition.columns)} cols')\n",
|
| 97 |
+
"print(f'products: {len(products)} rows, {len(products.columns)} cols')"
|
| 98 |
+
]
|
| 99 |
+
},
|
| 100 |
+
{
|
| 101 |
+
"cell_type": "markdown",
|
| 102 |
+
"metadata": {},
|
| 103 |
+
"source": [
|
| 104 |
+
"## 2. Nutrition Schema"
|
| 105 |
+
]
|
| 106 |
+
},
|
| 107 |
+
{
|
| 108 |
+
"cell_type": "code",
|
| 109 |
+
"execution_count": 3,
|
| 110 |
+
"metadata": {
|
| 111 |
+
"execution": {
|
| 112 |
+
"iopub.execute_input": "2026-07-30T18:23:34.404066Z",
|
| 113 |
+
"iopub.status.busy": "2026-07-30T18:23:34.403187Z",
|
| 114 |
+
"iopub.status.idle": "2026-07-30T18:23:34.456397Z",
|
| 115 |
+
"shell.execute_reply": "2026-07-30T18:23:34.454663Z"
|
| 116 |
+
}
|
| 117 |
+
},
|
| 118 |
+
"outputs": [
|
| 119 |
+
{
|
| 120 |
+
"name": "stdout",
|
| 121 |
+
"output_type": "stream",
|
| 122 |
+
"text": [
|
| 123 |
+
"Rows: 4440\n",
|
| 124 |
+
"Columns: 21\n",
|
| 125 |
+
"\n",
|
| 126 |
+
"Column details:\n",
|
| 127 |
+
" upc str null= 1 ( 0.0%) unique=3271\n",
|
| 128 |
+
" external_id str null= 0 ( 0.0%) unique=4440\n",
|
| 129 |
+
" title str null= 0 ( 0.0%) unique=4375\n",
|
| 130 |
+
" serving_size str null= 1505 (33.9%) unique=566\n",
|
| 131 |
+
" calories float64 null= 1661 (37.4%) unique=73\n",
|
| 132 |
+
" carbohydrate_g float64 null= 1876 (42.3%) unique=89\n",
|
| 133 |
+
" sugars_g float64 null= 2466 (55.5%) unique=53\n",
|
| 134 |
+
" sodium_mg float64 null= 1959 (44.1%) unique=131\n",
|
| 135 |
+
" potassium_mg float64 null= 1891 (42.6%) unique=39\n",
|
| 136 |
+
" fat_g float64 null= 2311 (52.0%) unique=53\n",
|
| 137 |
+
" saturated_fat_g float64 null= 2467 (55.6%) unique=30\n",
|
| 138 |
+
" polyunsaturated_fat_g float64 null= 4343 (97.8%) unique=20\n",
|
| 139 |
+
" omega6_g float64 null= 4368 (98.4%) unique=15\n",
|
| 140 |
+
" omega3_g float64 null= 4377 (98.6%) unique=15\n",
|
| 141 |
+
" monounsaturated_fat_g float64 null= 4352 (98.0%) unique=21\n",
|
| 142 |
+
" sugar_alcohols_g float64 null= 4329 (97.5%) unique=2\n",
|
| 143 |
+
" fibre_g float64 null= 2852 (64.2%) unique=19\n",
|
| 144 |
+
" protein_g float64 null= 1887 (42.5%) unique=45\n",
|
| 145 |
+
" calcium_mg float64 null= 3225 (72.6%) unique=19\n",
|
| 146 |
+
" iron_mg float64 null= 3281 (73.9%) unique=27\n",
|
| 147 |
+
" cholesterol_mg float64 null= 3309 (74.5%) unique=40\n"
|
| 148 |
+
]
|
| 149 |
+
}
|
| 150 |
+
],
|
| 151 |
+
"source": [
|
| 152 |
+
"print(f'Rows: {len(nutrition)}')\n",
|
| 153 |
+
"print(f'Columns: {len(nutrition.columns)}')\n",
|
| 154 |
+
"print(f'\\nColumn details:')\n",
|
| 155 |
+
"for col in nutrition.columns:\n",
|
| 156 |
+
" dtype = str(nutrition[col].dtype)\n",
|
| 157 |
+
" null_count = int(nutrition[col].isna().sum())\n",
|
| 158 |
+
" null_pct = null_count / len(nutrition) * 100\n",
|
| 159 |
+
" nuniq = int(nutrition[col].nunique())\n",
|
| 160 |
+
" print(f' {col:40s} {dtype:15s} null={null_count:5d} ({null_pct:4.1f}%) unique={nuniq}')"
|
| 161 |
+
]
|
| 162 |
+
},
|
| 163 |
+
{
|
| 164 |
+
"cell_type": "markdown",
|
| 165 |
+
"metadata": {},
|
| 166 |
+
"source": [
|
| 167 |
+
"## 3. Product-Nutrition Relationship"
|
| 168 |
+
]
|
| 169 |
+
},
|
| 170 |
+
{
|
| 171 |
+
"cell_type": "code",
|
| 172 |
+
"execution_count": 4,
|
| 173 |
+
"metadata": {
|
| 174 |
+
"execution": {
|
| 175 |
+
"iopub.execute_input": "2026-07-30T18:23:34.465862Z",
|
| 176 |
+
"iopub.status.busy": "2026-07-30T18:23:34.465322Z",
|
| 177 |
+
"iopub.status.idle": "2026-07-30T18:23:34.553322Z",
|
| 178 |
+
"shell.execute_reply": "2026-07-30T18:23:34.550424Z"
|
| 179 |
+
}
|
| 180 |
+
},
|
| 181 |
+
"outputs": [
|
| 182 |
+
{
|
| 183 |
+
"name": "stdout",
|
| 184 |
+
"output_type": "stream",
|
| 185 |
+
"text": [
|
| 186 |
+
"Products: 4440 unique external_ids\n",
|
| 187 |
+
"Nutrition: 4440 unique external_ids\n",
|
| 188 |
+
"Matching: 4440\n",
|
| 189 |
+
"Products without nutrition: 0\n",
|
| 190 |
+
"Nutrition without product: 0\n",
|
| 191 |
+
"\n",
|
| 192 |
+
"Records per product: mean=1.00, max=1\n",
|
| 193 |
+
"Products with 1 record: 4440\n",
|
| 194 |
+
"Products with 2+ records: 0\n",
|
| 195 |
+
"\n",
|
| 196 |
+
"=== RELATIONSHIP: 1:1 (perfect match by external_id) ===\n"
|
| 197 |
+
]
|
| 198 |
+
}
|
| 199 |
+
],
|
| 200 |
+
"source": [
|
| 201 |
+
"products_eids = set(products['external_id'].astype(str).unique())\n",
|
| 202 |
+
"nutrition_eids = set(nutrition['external_id'].astype(str).unique())\n",
|
| 203 |
+
"\n",
|
| 204 |
+
"print(f'Products: {len(products_eids)} unique external_ids')\n",
|
| 205 |
+
"print(f'Nutrition: {len(nutrition_eids)} unique external_ids')\n",
|
| 206 |
+
"print(f'Matching: {len(products_eids & nutrition_eids)}')\n",
|
| 207 |
+
"print(f'Products without nutrition: {len(products_eids - nutrition_eids)}')\n",
|
| 208 |
+
"print(f'Nutrition without product: {len(nutrition_eids - products_eids)}')\n",
|
| 209 |
+
"\n",
|
| 210 |
+
"nutrition_per_product = nutrition.groupby('external_id').size()\n",
|
| 211 |
+
"print(f'\\nRecords per product: mean={nutrition_per_product.mean():.2f}, max={nutrition_per_product.max()}')\n",
|
| 212 |
+
"print(f'Products with 1 record: {(nutrition_per_product == 1).sum()}')\n",
|
| 213 |
+
"print(f'Products with 2+ records: {(nutrition_per_product > 1).sum()}')\n",
|
| 214 |
+
"\n",
|
| 215 |
+
"print(f'\\n=== RELATIONSHIP: 1:1 (perfect match by external_id) ===')"
|
| 216 |
+
]
|
| 217 |
+
},
|
| 218 |
+
{
|
| 219 |
+
"cell_type": "markdown",
|
| 220 |
+
"metadata": {},
|
| 221 |
+
"source": [
|
| 222 |
+
"## 4. Matching Quality Table"
|
| 223 |
+
]
|
| 224 |
+
},
|
| 225 |
+
{
|
| 226 |
+
"cell_type": "code",
|
| 227 |
+
"execution_count": 5,
|
| 228 |
+
"metadata": {
|
| 229 |
+
"execution": {
|
| 230 |
+
"iopub.execute_input": "2026-07-30T18:23:34.559953Z",
|
| 231 |
+
"iopub.status.busy": "2026-07-30T18:23:34.559296Z",
|
| 232 |
+
"iopub.status.idle": "2026-07-30T18:23:34.603930Z",
|
| 233 |
+
"shell.execute_reply": "2026-07-30T18:23:34.600833Z"
|
| 234 |
+
}
|
| 235 |
+
},
|
| 236 |
+
"outputs": [
|
| 237 |
+
{
|
| 238 |
+
"name": "stdout",
|
| 239 |
+
"output_type": "stream",
|
| 240 |
+
"text": [
|
| 241 |
+
" method records pct confidence\n",
|
| 242 |
+
" external_id exact 4440 100.0% HIGH\n",
|
| 243 |
+
" UPC exact 3271 73.7% HIGH\n",
|
| 244 |
+
" other identifier 0 0.0% \n",
|
| 245 |
+
"deterministic composite 0 0.0% \n",
|
| 246 |
+
" unresolved 0 0.0% \n"
|
| 247 |
+
]
|
| 248 |
+
}
|
| 249 |
+
],
|
| 250 |
+
"source": [
|
| 251 |
+
"products_upcs = set(products['upc'].astype(str).dropna().unique())\n",
|
| 252 |
+
"nutrition_upcs = set(nutrition['upc'].astype(str).dropna().unique())\n",
|
| 253 |
+
"\n",
|
| 254 |
+
"match_data = [\n",
|
| 255 |
+
" {'method': 'external_id exact', 'records': len(products_eids & nutrition_eids), 'pct': f'{len(products_eids & nutrition_eids)/len(nutrition)*100:.1f}%', 'confidence': 'HIGH'},\n",
|
| 256 |
+
" {'method': 'UPC exact', 'records': len(products_upcs & nutrition_upcs), 'pct': f'{len(products_upcs & nutrition_upcs)/len(nutrition)*100:.1f}%', 'confidence': 'HIGH'},\n",
|
| 257 |
+
" {'method': 'other identifier', 'records': 0, 'pct': '0.0%', 'confidence': ''},\n",
|
| 258 |
+
" {'method': 'deterministic composite', 'records': 0, 'pct': '0.0%', 'confidence': ''},\n",
|
| 259 |
+
" {'method': 'unresolved', 'records': 0, 'pct': '0.0%', 'confidence': ''},\n",
|
| 260 |
+
"]\n",
|
| 261 |
+
"match_df = pd.DataFrame(match_data)\n",
|
| 262 |
+
"print(match_df.to_string(index=False))"
|
| 263 |
+
]
|
| 264 |
+
},
|
| 265 |
+
{
|
| 266 |
+
"cell_type": "markdown",
|
| 267 |
+
"metadata": {},
|
| 268 |
+
"source": [
|
| 269 |
+
"## 5. Suspicious Values"
|
| 270 |
+
]
|
| 271 |
+
},
|
| 272 |
+
{
|
| 273 |
+
"cell_type": "code",
|
| 274 |
+
"execution_count": 6,
|
| 275 |
+
"metadata": {
|
| 276 |
+
"execution": {
|
| 277 |
+
"iopub.execute_input": "2026-07-30T18:23:34.610018Z",
|
| 278 |
+
"iopub.status.busy": "2026-07-30T18:23:34.609409Z",
|
| 279 |
+
"iopub.status.idle": "2026-07-30T18:23:34.639575Z",
|
| 280 |
+
"shell.execute_reply": "2026-07-30T18:23:34.636915Z"
|
| 281 |
+
}
|
| 282 |
+
},
|
| 283 |
+
"outputs": [
|
| 284 |
+
{
|
| 285 |
+
"name": "stdout",
|
| 286 |
+
"output_type": "stream",
|
| 287 |
+
"text": [
|
| 288 |
+
"calories > 1000:\n"
|
| 289 |
+
]
|
| 290 |
+
},
|
| 291 |
+
{
|
| 292 |
+
"name": "stdout",
|
| 293 |
+
"output_type": "stream",
|
| 294 |
+
"text": [
|
| 295 |
+
" 733225EA: Compliments Mushroom Whole White 680 g... calories=1000000.0\n",
|
| 296 |
+
" 218890KG: Compliments Whole Chicken Grade A... calories=2100.0\n",
|
| 297 |
+
"\n",
|
| 298 |
+
"fat_g > 100:\n",
|
| 299 |
+
" 274613EA: Compliments Extra Old Cheddar Cheese 300 g... fat_g=710.0\n",
|
| 300 |
+
" 218890KG: Compliments Whole Chicken Grade A... fat_g=150.0\n",
|
| 301 |
+
"\n",
|
| 302 |
+
"protein_g > 100:\n",
|
| 303 |
+
" 218890KG: Compliments Whole Chicken Grade A... protein_g=190.0\n",
|
| 304 |
+
"\n",
|
| 305 |
+
"Records with ALL 17 nutrition fields null: 1505\n"
|
| 306 |
+
]
|
| 307 |
+
}
|
| 308 |
+
],
|
| 309 |
+
"source": [
|
| 310 |
+
"print('calories > 1000:')\n",
|
| 311 |
+
"for _, row in nutrition[nutrition['calories'] > 1000].iterrows():\n",
|
| 312 |
+
" print(f' {row[\"external_id\"]}: {row[\"title\"][:50]}... calories={row[\"calories\"]}')\n",
|
| 313 |
+
"\n",
|
| 314 |
+
"print('\\nfat_g > 100:')\n",
|
| 315 |
+
"for _, row in nutrition[nutrition['fat_g'] > 100].iterrows():\n",
|
| 316 |
+
" print(f' {row[\"external_id\"]}: {row[\"title\"][:50]}... fat_g={row[\"fat_g\"]}')\n",
|
| 317 |
+
"\n",
|
| 318 |
+
"print('\\nprotein_g > 100:')\n",
|
| 319 |
+
"for _, row in nutrition[nutrition['protein_g'] > 100].iterrows():\n",
|
| 320 |
+
" print(f' {row[\"external_id\"]}: {row[\"title\"][:50]}... protein_g={row[\"protein_g\"]}')\n",
|
| 321 |
+
"\n",
|
| 322 |
+
"null_fields = [\"calories\",\"carbohydrate_g\",\"sugars_g\",\"sodium_mg\",\"potassium_mg\",\"fat_g\",\"saturated_fat_g\",\"protein_g\",\"fibre_g\",\"calcium_mg\",\"iron_mg\",\"cholesterol_mg\"]\n",
|
| 323 |
+
"all_null = nutrition[null_fields].isna().all(axis=1).sum()\n",
|
| 324 |
+
"print(f'\\nRecords with ALL 17 nutrition fields null: {all_null}')"
|
| 325 |
+
]
|
| 326 |
+
},
|
| 327 |
+
{
|
| 328 |
+
"cell_type": "markdown",
|
| 329 |
+
"metadata": {},
|
| 330 |
+
"source": [
|
| 331 |
+
"## 6. Serving Size Analysis"
|
| 332 |
+
]
|
| 333 |
+
},
|
| 334 |
+
{
|
| 335 |
+
"cell_type": "code",
|
| 336 |
+
"execution_count": 7,
|
| 337 |
+
"metadata": {
|
| 338 |
+
"execution": {
|
| 339 |
+
"iopub.execute_input": "2026-07-30T18:23:34.645023Z",
|
| 340 |
+
"iopub.status.busy": "2026-07-30T18:23:34.644442Z",
|
| 341 |
+
"iopub.status.idle": "2026-07-30T18:23:34.671020Z",
|
| 342 |
+
"shell.execute_reply": "2026-07-30T18:23:34.667988Z"
|
| 343 |
+
}
|
| 344 |
+
},
|
| 345 |
+
"outputs": [
|
| 346 |
+
{
|
| 347 |
+
"name": "stdout",
|
| 348 |
+
"output_type": "stream",
|
| 349 |
+
"text": [
|
| 350 |
+
"Non-null serving_size: 2935 / 4440\n",
|
| 351 |
+
"Unique: 566\n",
|
| 352 |
+
"\n",
|
| 353 |
+
"Top 10 serving sizes:\n",
|
| 354 |
+
" \"100 g\": 148 records\n",
|
| 355 |
+
" \"50 g\": 125 records\n",
|
| 356 |
+
" \"85 g\": 111 records\n",
|
| 357 |
+
" \"30 g\": 107 records\n",
|
| 358 |
+
" \"125 mL\": 79 records\n",
|
| 359 |
+
" \"\": 56 records\n",
|
| 360 |
+
" \"1 cup (250 mL)\": 52 records\n",
|
| 361 |
+
" \"3 cm cube (30 g)\": 51 records\n",
|
| 362 |
+
" \"140 g\": 51 records\n",
|
| 363 |
+
" \"40 g\": 50 records\n",
|
| 364 |
+
"\n",
|
| 365 |
+
"Records with serving_size = \"100 g\": 148\n",
|
| 366 |
+
" calories range: 20.0 - 2100.0\n"
|
| 367 |
+
]
|
| 368 |
+
}
|
| 369 |
+
],
|
| 370 |
+
"source": [
|
| 371 |
+
"ss = nutrition['serving_size'].dropna()\n",
|
| 372 |
+
"print(f'Non-null serving_size: {len(ss)} / {len(nutrition)}')\n",
|
| 373 |
+
"print(f'Unique: {ss.nunique()}')\n",
|
| 374 |
+
"print(f'\\nTop 10 serving sizes:')\n",
|
| 375 |
+
"for v, c in ss.value_counts().head(10).items():\n",
|
| 376 |
+
" print(f' \"{v}\": {c} records')\n",
|
| 377 |
+
"\n",
|
| 378 |
+
"per_100g = nutrition[nutrition['serving_size'] == '100 g']\n",
|
| 379 |
+
"print(f'\\nRecords with serving_size = \"100 g\": {len(per_100g)}')\n",
|
| 380 |
+
"if len(per_100g) > 0:\n",
|
| 381 |
+
" print(f' calories range: {per_100g[\"calories\"].min()} - {per_100g[\"calories\"].max()}')"
|
| 382 |
+
]
|
| 383 |
+
},
|
| 384 |
+
{
|
| 385 |
+
"cell_type": "markdown",
|
| 386 |
+
"metadata": {},
|
| 387 |
+
"source": [
|
| 388 |
+
"## 7. Sample Examples"
|
| 389 |
+
]
|
| 390 |
+
},
|
| 391 |
+
{
|
| 392 |
+
"cell_type": "code",
|
| 393 |
+
"execution_count": 8,
|
| 394 |
+
"metadata": {
|
| 395 |
+
"execution": {
|
| 396 |
+
"iopub.execute_input": "2026-07-30T18:23:34.677987Z",
|
| 397 |
+
"iopub.status.busy": "2026-07-30T18:23:34.677342Z",
|
| 398 |
+
"iopub.status.idle": "2026-07-30T18:23:34.702762Z",
|
| 399 |
+
"shell.execute_reply": "2026-07-30T18:23:34.701131Z"
|
| 400 |
+
}
|
| 401 |
+
},
|
| 402 |
+
"outputs": [
|
| 403 |
+
{
|
| 404 |
+
"name": "stdout",
|
| 405 |
+
"output_type": "stream",
|
| 406 |
+
"text": [
|
| 407 |
+
"=== Successful exact match ===\n",
|
| 408 |
+
" external_id: 484125EA\n",
|
| 409 |
+
" title: Compliments Naturally Simple Peanut Butter Crunchy 100% Natural 500 g\n",
|
| 410 |
+
" serving_size: nan\n",
|
| 411 |
+
" calories: nan\n",
|
| 412 |
+
" fat_g: nan\n",
|
| 413 |
+
" protein_g: nan\n",
|
| 414 |
+
"\n",
|
| 415 |
+
"=== Most complete record ===\n",
|
| 416 |
+
" external_id: 6207EA\n",
|
| 417 |
+
" title: Compliments Frozen Haddock Fillets Battered 575 g\n",
|
| 418 |
+
" serving_size: 1 piece (100 g)\n",
|
| 419 |
+
" calories: 250.0\n",
|
| 420 |
+
" All fields: 17/17\n"
|
| 421 |
+
]
|
| 422 |
+
}
|
| 423 |
+
],
|
| 424 |
+
"source": [
|
| 425 |
+
"print('=== Successful exact match ===')\n",
|
| 426 |
+
"sample = nutrition[nutrition['external_id'] == '484125EA'].iloc[0]\n",
|
| 427 |
+
"print(f' external_id: {sample[\"external_id\"]}')\n",
|
| 428 |
+
"print(f' title: {sample[\"title\"]}')\n",
|
| 429 |
+
"print(f' serving_size: {sample[\"serving_size\"]}')\n",
|
| 430 |
+
"print(f' calories: {sample[\"calories\"]}')\n",
|
| 431 |
+
"print(f' fat_g: {sample[\"fat_g\"]}')\n",
|
| 432 |
+
"print(f' protein_g: {sample[\"protein_g\"]}')\n",
|
| 433 |
+
"\n",
|
| 434 |
+
"print('\\n=== Most complete record ===')\n",
|
| 435 |
+
"nonnull_counts = nutrition.drop(columns=['upc', 'external_id', 'title', 'serving_size']).notna().sum(axis=1)\n",
|
| 436 |
+
"best_idx = nonnull_counts.idxmax()\n",
|
| 437 |
+
"sample = nutrition.loc[best_idx]\n",
|
| 438 |
+
"print(f' external_id: {sample[\"external_id\"]}')\n",
|
| 439 |
+
"print(f' title: {sample[\"title\"]}')\n",
|
| 440 |
+
"print(f' serving_size: {sample[\"serving_size\"]}')\n",
|
| 441 |
+
"print(f' calories: {sample[\"calories\"]}')\n",
|
| 442 |
+
"print(f' All fields: {nonnull_counts.max()}/17')"
|
| 443 |
+
]
|
| 444 |
+
},
|
| 445 |
+
{
|
| 446 |
+
"cell_type": "markdown",
|
| 447 |
+
"metadata": {},
|
| 448 |
+
"source": [
|
| 449 |
+
"## 8. Save Audit Files"
|
| 450 |
+
]
|
| 451 |
+
},
|
| 452 |
+
{
|
| 453 |
+
"cell_type": "code",
|
| 454 |
+
"execution_count": 9,
|
| 455 |
+
"metadata": {
|
| 456 |
+
"execution": {
|
| 457 |
+
"iopub.execute_input": "2026-07-30T18:23:34.706952Z",
|
| 458 |
+
"iopub.status.busy": "2026-07-30T18:23:34.706332Z",
|
| 459 |
+
"iopub.status.idle": "2026-07-30T18:23:34.719947Z",
|
| 460 |
+
"shell.execute_reply": "2026-07-30T18:23:34.718345Z"
|
| 461 |
+
}
|
| 462 |
+
},
|
| 463 |
+
"outputs": [
|
| 464 |
+
{
|
| 465 |
+
"name": "stdout",
|
| 466 |
+
"output_type": "stream",
|
| 467 |
+
"text": [
|
| 468 |
+
"Saved audit/nutrition_matching_quality.csv\n",
|
| 469 |
+
"\n",
|
| 470 |
+
"Audit complete.\n"
|
| 471 |
+
]
|
| 472 |
+
}
|
| 473 |
+
],
|
| 474 |
+
"source": [
|
| 475 |
+
"import os\n",
|
| 476 |
+
"os.makedirs('audit', exist_ok=True)\n",
|
| 477 |
+
"\n",
|
| 478 |
+
"match_df.to_csv('audit/nutrition_matching_quality.csv', index=False)\n",
|
| 479 |
+
"print('Saved audit/nutrition_matching_quality.csv')\n",
|
| 480 |
+
"\n",
|
| 481 |
+
"print('\\nAudit complete.')"
|
| 482 |
+
]
|
| 483 |
+
}
|
| 484 |
+
],
|
| 485 |
+
"metadata": {
|
| 486 |
+
"kernelspec": {
|
| 487 |
+
"display_name": "Python 3",
|
| 488 |
+
"language": "python",
|
| 489 |
+
"name": "python3"
|
| 490 |
+
},
|
| 491 |
+
"language_info": {
|
| 492 |
+
"codemirror_mode": {
|
| 493 |
+
"name": "ipython",
|
| 494 |
+
"version": 3
|
| 495 |
+
},
|
| 496 |
+
"file_extension": ".py",
|
| 497 |
+
"mimetype": "text/x-python",
|
| 498 |
+
"name": "python",
|
| 499 |
+
"nbconvert_exporter": "python",
|
| 500 |
+
"pygments_lexer": "ipython3",
|
| 501 |
+
"version": "3.13.7"
|
| 502 |
+
}
|
| 503 |
+
},
|
| 504 |
+
"nbformat": 4,
|
| 505 |
+
"nbformat_minor": 4
|
| 506 |
+
}
|