File size: 4,572 Bytes
5946309
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "57bbc684",
   "metadata": {},
   "source": [
    "# Global Packaged Food\n",
    "\n",
    "Starter notebook for the Kaggle dataset in this folder. It loads `food_products.csv` with pandas and walks through quality checks and a first piece of analysis.\n",
    "\n",
    "**Files:** `food_products.csv`, `data_dictionary.csv`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0000f7f2",
   "metadata": {},
   "source": [
    "## 1. Load the data"
   ]
  },
  {
   "cell_type": "code",
   "id": "b69a57ed",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv(\"food_products.csv\", low_memory=False)\n",
    "print(df.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "325de123",
   "metadata": {},
   "source": [
    "## 2. Shape, types and missing values"
   ]
  },
  {
   "cell_type": "code",
   "id": "6eb42bc8",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "print(f\"{df.shape[0]:,} rows x {df.shape[1]} columns\")\n",
    "\n",
    "missing = df.isna().mean().mul(100).sort_values(ascending=False)\n",
    "missing[missing > 0].round(1).to_frame(\"missing_%\").head(15)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2d963fe",
   "metadata": {},
   "source": [
    "## 3. Numeric summary"
   ]
  },
  {
   "cell_type": "code",
   "id": "aa2f8fd4",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "df.describe(include='number').T.round(2).head(20)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01599df7",
   "metadata": {},
   "source": [
    "## 4. Nutri-Score distribution"
   ]
  },
  {
   "cell_type": "code",
   "id": "476cae43",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "df[\"nutriscore_grade\"].value_counts().reindex([\"a\", \"b\", \"c\", \"d\", \"e\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "400d09cc",
   "metadata": {},
   "source": [
    "## 5. Sugar by category"
   ]
  },
  {
   "cell_type": "code",
   "id": "1ea48825",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "df.groupby(\"categories_tags\")[\"sugars_100g\"].median().dropna().sort_values().tail(12)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "164b1811",
   "metadata": {},
   "source": [
    "## 6. Ultra-processed share"
   ]
  },
  {
   "cell_type": "code",
   "id": "7ebaa7b2",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "df[\"nova_group\"].value_counts().sort_index().to_frame(\"products\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "726326cf",
   "metadata": {},
   "source": [
    "## 7. Nutrients vs Nutri-Score"
   ]
  },
  {
   "cell_type": "code",
   "id": "a306ec46",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "df.boxplot(column=\"sugars_100g\", by=\"nutriscore_grade\", figsize=(9, 4))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46abb4b7",
   "metadata": {},
   "source": [
    "## 8. Salt vs sugar"
   ]
  },
  {
   "cell_type": "code",
   "id": "351ccb6e",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "sub = df.dropna(subset=[\"salt_100g\", \"sugars_100g\"]).query(\"salt_100g < 10 and sugars_100g < 60\")\n",
    "sub.plot.scatter(x=\"sugars_100g\", y=\"salt_100g\", s=4, alpha=0.25, figsize=(8, 5))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7db7244",
   "metadata": {},
   "source": [
    "## 9. Label prevalence"
   ]
  },
  {
   "cell_type": "code",
   "id": "be5efe14",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "for col in [\"is_organic\", \"is_vegan\", \"is_vegetarian\", \"is_gluten_free\"]:\n",
    "    print(col, round((df[col].astype(str).str.lower() == \"true\").mean() * 100, 1), \"%\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e43770c",
   "metadata": {},
   "source": [
    "## Next steps\n",
    "\n",
    "See `KAGGLE.md` for analysis ideas, caveats and provenance."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.11",
   "mimetype": "text/x-python",
   "file_extension": ".py",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}