sguev05 commited on
Commit
705e843
·
verified ·
1 Parent(s): 8dd498c

Upload 1_Data_Creation_(2).ipynb

Browse files
Files changed (1) hide show
  1. 1_Data_Creation_(2).ipynb +2101 -0
1_Data_Creation_(2).ipynb ADDED
@@ -0,0 +1,2101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {
6
+ "id": "4ba6aba8"
7
+ },
8
+ "source": [
9
+ "# 🤖 **Data Collection, Creation, Storage, and Processing**\n"
10
+ ]
11
+ },
12
+ {
13
+ "cell_type": "markdown",
14
+ "metadata": {
15
+ "id": "jpASMyIQMaAq"
16
+ },
17
+ "source": [
18
+ "## **1.** 📦 Install required packages"
19
+ ]
20
+ },
21
+ {
22
+ "cell_type": "code",
23
+ "execution_count": null,
24
+ "metadata": {
25
+ "colab": {
26
+ "base_uri": "https://localhost:8080/"
27
+ },
28
+ "id": "f48c8f8c",
29
+ "outputId": "5eab5d3d-d345-4d7d-b644-d7c5bc55c88d"
30
+ },
31
+ "outputs": [
32
+ {
33
+ "output_type": "stream",
34
+ "name": "stdout",
35
+ "text": [
36
+ "Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.12/dist-packages (4.13.5)\n",
37
+ "Requirement already satisfied: pandas in /usr/local/lib/python3.12/dist-packages (2.2.2)\n",
38
+ "Requirement already satisfied: matplotlib in /usr/local/lib/python3.12/dist-packages (3.10.0)\n",
39
+ "Requirement already satisfied: seaborn in /usr/local/lib/python3.12/dist-packages (0.13.2)\n",
40
+ "Requirement already satisfied: numpy in /usr/local/lib/python3.12/dist-packages (2.0.2)\n",
41
+ "Requirement already satisfied: textblob in /usr/local/lib/python3.12/dist-packages (0.19.0)\n",
42
+ "Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.12/dist-packages (from beautifulsoup4) (2.8.3)\n",
43
+ "Requirement already satisfied: typing-extensions>=4.0.0 in /usr/local/lib/python3.12/dist-packages (from beautifulsoup4) (4.15.0)\n",
44
+ "Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.12/dist-packages (from pandas) (2.9.0.post0)\n",
45
+ "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.12/dist-packages (from pandas) (2025.2)\n",
46
+ "Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.12/dist-packages (from pandas) (2025.3)\n",
47
+ "Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (1.3.3)\n",
48
+ "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (0.12.1)\n",
49
+ "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (4.62.0)\n",
50
+ "Requirement already satisfied: kiwisolver>=1.3.1 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (1.5.0)\n",
51
+ "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (26.0)\n",
52
+ "Requirement already satisfied: pillow>=8 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (11.3.0)\n",
53
+ "Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (3.3.2)\n",
54
+ "Requirement already satisfied: nltk>=3.9 in /usr/local/lib/python3.12/dist-packages (from textblob) (3.9.1)\n",
55
+ "Requirement already satisfied: click in /usr/local/lib/python3.12/dist-packages (from nltk>=3.9->textblob) (8.3.1)\n",
56
+ "Requirement already satisfied: joblib in /usr/local/lib/python3.12/dist-packages (from nltk>=3.9->textblob) (1.5.3)\n",
57
+ "Requirement already satisfied: regex>=2021.8.3 in /usr/local/lib/python3.12/dist-packages (from nltk>=3.9->textblob) (2025.11.3)\n",
58
+ "Requirement already satisfied: tqdm in /usr/local/lib/python3.12/dist-packages (from nltk>=3.9->textblob) (4.67.3)\n",
59
+ "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.12/dist-packages (from python-dateutil>=2.8.2->pandas) (1.17.0)\n"
60
+ ]
61
+ }
62
+ ],
63
+ "source": [
64
+ "!pip install beautifulsoup4 pandas matplotlib seaborn numpy textblob"
65
+ ]
66
+ },
67
+ {
68
+ "cell_type": "markdown",
69
+ "metadata": {
70
+ "id": "lquNYCbfL9IM"
71
+ },
72
+ "source": [
73
+ "## **2.** ⛏ Web-scrape all book titles, prices, and ratings from books.toscrape.com"
74
+ ]
75
+ },
76
+ {
77
+ "cell_type": "markdown",
78
+ "metadata": {
79
+ "id": "0IWuNpxxYDJF"
80
+ },
81
+ "source": [
82
+ "### *a. Initial setup*\n",
83
+ "Define the base url of the website you will scrape as well as how and what you will scrape"
84
+ ]
85
+ },
86
+ {
87
+ "cell_type": "code",
88
+ "execution_count": null,
89
+ "metadata": {
90
+ "id": "91d52125"
91
+ },
92
+ "outputs": [],
93
+ "source": [
94
+ "import requests\n",
95
+ "from bs4 import BeautifulSoup\n",
96
+ "import pandas as pd\n",
97
+ "import time\n",
98
+ "\n",
99
+ "base_url = \"https://books.toscrape.com/catalogue/page-{}.html\"\n",
100
+ "headers = {\"User-Agent\": \"Mozilla/5.0\"}\n",
101
+ "\n",
102
+ "titles, prices, ratings = [], [], []"
103
+ ]
104
+ },
105
+ {
106
+ "cell_type": "markdown",
107
+ "metadata": {
108
+ "id": "oCdTsin2Yfp3"
109
+ },
110
+ "source": [
111
+ "### *b. Fill titles, prices, and ratings from the web pages*"
112
+ ]
113
+ },
114
+ {
115
+ "cell_type": "code",
116
+ "execution_count": null,
117
+ "metadata": {
118
+ "id": "xqO5Y3dnYhxt"
119
+ },
120
+ "outputs": [],
121
+ "source": [
122
+ "# Loop through all 50 pages\n",
123
+ "for page in range(1, 51):\n",
124
+ " url = base_url.format(page)\n",
125
+ " response = requests.get(url, headers=headers)\n",
126
+ " soup = BeautifulSoup(response.content, \"html.parser\")\n",
127
+ " books = soup.find_all(\"article\", class_=\"product_pod\")\n",
128
+ "\n",
129
+ " for book in books:\n",
130
+ " titles.append(book.h3.a[\"title\"])\n",
131
+ " prices.append(float(book.find(\"p\", class_=\"price_color\").text[1:]))\n",
132
+ " ratings.append(book.p.get(\"class\")[1])\n",
133
+ "\n",
134
+ " time.sleep(0.5) # polite scraping delay"
135
+ ]
136
+ },
137
+ {
138
+ "cell_type": "code",
139
+ "source": [
140
+ "len(titles), len(prices), len(ratings)"
141
+ ],
142
+ "metadata": {
143
+ "colab": {
144
+ "base_uri": "https://localhost:8080/"
145
+ },
146
+ "id": "2RfLW0u7iksi",
147
+ "outputId": "691adf75-63ef-4c4d-9f1a-1690c5d97a5c"
148
+ },
149
+ "execution_count": null,
150
+ "outputs": [
151
+ {
152
+ "output_type": "execute_result",
153
+ "data": {
154
+ "text/plain": [
155
+ "(1000, 1000, 1000)"
156
+ ]
157
+ },
158
+ "metadata": {},
159
+ "execution_count": 6
160
+ }
161
+ ]
162
+ },
163
+ {
164
+ "cell_type": "markdown",
165
+ "metadata": {
166
+ "id": "T0TOeRC4Yrnn"
167
+ },
168
+ "source": [
169
+ "### *c. ✋🏻🛑⛔️ Create a dataframe df_books that contains the now complete \"title\", \"price\", and \"rating\" objects*"
170
+ ]
171
+ },
172
+ {
173
+ "cell_type": "code",
174
+ "execution_count": null,
175
+ "metadata": {
176
+ "id": "l5FkkNhUYTHh",
177
+ "colab": {
178
+ "base_uri": "https://localhost:8080/",
179
+ "height": 204
180
+ },
181
+ "outputId": "bf5b4f2d-330c-485d-ea69-ee8302d19be0"
182
+ },
183
+ "outputs": [
184
+ {
185
+ "output_type": "execute_result",
186
+ "data": {
187
+ "text/plain": [
188
+ " title price rating\n",
189
+ "0 A Light in the Attic 51.77 Three\n",
190
+ "1 Tipping the Velvet 53.74 One\n",
191
+ "2 Soumission 50.10 One\n",
192
+ "3 Sharp Objects 47.82 Four\n",
193
+ "4 Sapiens: A Brief History of Humankind 54.23 Five"
194
+ ],
195
+ "text/html": [
196
+ "\n",
197
+ " <div id=\"df-20f49f97-f225-466e-a44a-36dfa28affe8\" class=\"colab-df-container\">\n",
198
+ " <div>\n",
199
+ "<style scoped>\n",
200
+ " .dataframe tbody tr th:only-of-type {\n",
201
+ " vertical-align: middle;\n",
202
+ " }\n",
203
+ "\n",
204
+ " .dataframe tbody tr th {\n",
205
+ " vertical-align: top;\n",
206
+ " }\n",
207
+ "\n",
208
+ " .dataframe thead th {\n",
209
+ " text-align: right;\n",
210
+ " }\n",
211
+ "</style>\n",
212
+ "<table border=\"1\" class=\"dataframe\">\n",
213
+ " <thead>\n",
214
+ " <tr style=\"text-align: right;\">\n",
215
+ " <th></th>\n",
216
+ " <th>title</th>\n",
217
+ " <th>price</th>\n",
218
+ " <th>rating</th>\n",
219
+ " </tr>\n",
220
+ " </thead>\n",
221
+ " <tbody>\n",
222
+ " <tr>\n",
223
+ " <th>0</th>\n",
224
+ " <td>A Light in the Attic</td>\n",
225
+ " <td>51.77</td>\n",
226
+ " <td>Three</td>\n",
227
+ " </tr>\n",
228
+ " <tr>\n",
229
+ " <th>1</th>\n",
230
+ " <td>Tipping the Velvet</td>\n",
231
+ " <td>53.74</td>\n",
232
+ " <td>One</td>\n",
233
+ " </tr>\n",
234
+ " <tr>\n",
235
+ " <th>2</th>\n",
236
+ " <td>Soumission</td>\n",
237
+ " <td>50.10</td>\n",
238
+ " <td>One</td>\n",
239
+ " </tr>\n",
240
+ " <tr>\n",
241
+ " <th>3</th>\n",
242
+ " <td>Sharp Objects</td>\n",
243
+ " <td>47.82</td>\n",
244
+ " <td>Four</td>\n",
245
+ " </tr>\n",
246
+ " <tr>\n",
247
+ " <th>4</th>\n",
248
+ " <td>Sapiens: A Brief History of Humankind</td>\n",
249
+ " <td>54.23</td>\n",
250
+ " <td>Five</td>\n",
251
+ " </tr>\n",
252
+ " </tbody>\n",
253
+ "</table>\n",
254
+ "</div>\n",
255
+ " <div class=\"colab-df-buttons\">\n",
256
+ "\n",
257
+ " <div class=\"colab-df-container\">\n",
258
+ " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-20f49f97-f225-466e-a44a-36dfa28affe8')\"\n",
259
+ " title=\"Convert this dataframe to an interactive table.\"\n",
260
+ " style=\"display:none;\">\n",
261
+ "\n",
262
+ " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
263
+ " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
264
+ " </svg>\n",
265
+ " </button>\n",
266
+ "\n",
267
+ " <style>\n",
268
+ " .colab-df-container {\n",
269
+ " display:flex;\n",
270
+ " gap: 12px;\n",
271
+ " }\n",
272
+ "\n",
273
+ " .colab-df-convert {\n",
274
+ " background-color: #E8F0FE;\n",
275
+ " border: none;\n",
276
+ " border-radius: 50%;\n",
277
+ " cursor: pointer;\n",
278
+ " display: none;\n",
279
+ " fill: #1967D2;\n",
280
+ " height: 32px;\n",
281
+ " padding: 0 0 0 0;\n",
282
+ " width: 32px;\n",
283
+ " }\n",
284
+ "\n",
285
+ " .colab-df-convert:hover {\n",
286
+ " background-color: #E2EBFA;\n",
287
+ " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
288
+ " fill: #174EA6;\n",
289
+ " }\n",
290
+ "\n",
291
+ " .colab-df-buttons div {\n",
292
+ " margin-bottom: 4px;\n",
293
+ " }\n",
294
+ "\n",
295
+ " [theme=dark] .colab-df-convert {\n",
296
+ " background-color: #3B4455;\n",
297
+ " fill: #D2E3FC;\n",
298
+ " }\n",
299
+ "\n",
300
+ " [theme=dark] .colab-df-convert:hover {\n",
301
+ " background-color: #434B5C;\n",
302
+ " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
303
+ " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
304
+ " fill: #FFFFFF;\n",
305
+ " }\n",
306
+ " </style>\n",
307
+ "\n",
308
+ " <script>\n",
309
+ " const buttonEl =\n",
310
+ " document.querySelector('#df-20f49f97-f225-466e-a44a-36dfa28affe8 button.colab-df-convert');\n",
311
+ " buttonEl.style.display =\n",
312
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
313
+ "\n",
314
+ " async function convertToInteractive(key) {\n",
315
+ " const element = document.querySelector('#df-20f49f97-f225-466e-a44a-36dfa28affe8');\n",
316
+ " const dataTable =\n",
317
+ " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
318
+ " [key], {});\n",
319
+ " if (!dataTable) return;\n",
320
+ "\n",
321
+ " const docLinkHtml = 'Like what you see? Visit the ' +\n",
322
+ " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
323
+ " + ' to learn more about interactive tables.';\n",
324
+ " element.innerHTML = '';\n",
325
+ " dataTable['output_type'] = 'display_data';\n",
326
+ " await google.colab.output.renderOutput(dataTable, element);\n",
327
+ " const docLink = document.createElement('div');\n",
328
+ " docLink.innerHTML = docLinkHtml;\n",
329
+ " element.appendChild(docLink);\n",
330
+ " }\n",
331
+ " </script>\n",
332
+ " </div>\n",
333
+ "\n",
334
+ "\n",
335
+ " </div>\n",
336
+ " </div>\n"
337
+ ],
338
+ "application/vnd.google.colaboratory.intrinsic+json": {
339
+ "type": "dataframe",
340
+ "variable_name": "df_books",
341
+ "summary": "{\n \"name\": \"df_books\",\n \"rows\": 1000,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 999,\n \"samples\": [\n \"The Grownup\",\n \"Persepolis: The Story of a Childhood (Persepolis #1-2)\",\n \"Ayumi's Violin\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"price\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 14.446689669952772,\n \"min\": 10.0,\n \"max\": 59.99,\n \"num_unique_values\": 903,\n \"samples\": [\n 19.73,\n 55.65,\n 46.31\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"rating\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 5,\n \"samples\": [\n \"One\",\n \"Two\",\n \"Four\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
342
+ }
343
+ },
344
+ "metadata": {},
345
+ "execution_count": 7
346
+ }
347
+ ],
348
+ "source": [
349
+ "df_books = pd.DataFrame({\n",
350
+ " \"title\": titles,\n",
351
+ " \"price\": prices,\n",
352
+ " \"rating\": ratings\n",
353
+ "})\n",
354
+ "\n",
355
+ "df_books.head()"
356
+ ]
357
+ },
358
+ {
359
+ "cell_type": "code",
360
+ "source": [
361
+ "df_books.to_csv(\"books.csv\", index=False)"
362
+ ],
363
+ "metadata": {
364
+ "id": "sWumcBu3i6Mj"
365
+ },
366
+ "execution_count": null,
367
+ "outputs": []
368
+ },
369
+ {
370
+ "cell_type": "code",
371
+ "source": [
372
+ "!ls"
373
+ ],
374
+ "metadata": {
375
+ "colab": {
376
+ "base_uri": "https://localhost:8080/"
377
+ },
378
+ "id": "CUPuxSv8jKoH",
379
+ "outputId": "c3dea3ed-d6e1-4e1d-f3ef-c0871abb5ecf"
380
+ },
381
+ "execution_count": null,
382
+ "outputs": [
383
+ {
384
+ "output_type": "stream",
385
+ "name": "stdout",
386
+ "text": [
387
+ "books.csv sample_data\n"
388
+ ]
389
+ }
390
+ ]
391
+ },
392
+ {
393
+ "cell_type": "code",
394
+ "source": [
395
+ "df_books.to_excel(\"books.xlsx\", index=False)"
396
+ ],
397
+ "metadata": {
398
+ "id": "R0qHyWBYjRb_"
399
+ },
400
+ "execution_count": null,
401
+ "outputs": []
402
+ },
403
+ {
404
+ "cell_type": "code",
405
+ "source": [
406
+ "!ls"
407
+ ],
408
+ "metadata": {
409
+ "colab": {
410
+ "base_uri": "https://localhost:8080/"
411
+ },
412
+ "id": "97_5ABW5jdgH",
413
+ "outputId": "0ef22899-9c10-4d95-d829-9380b7163c76"
414
+ },
415
+ "execution_count": null,
416
+ "outputs": [
417
+ {
418
+ "output_type": "stream",
419
+ "name": "stdout",
420
+ "text": [
421
+ "books.csv books.xlsx sample_data\n"
422
+ ]
423
+ }
424
+ ]
425
+ },
426
+ {
427
+ "cell_type": "code",
428
+ "source": [
429
+ "df_books.tail()"
430
+ ],
431
+ "metadata": {
432
+ "colab": {
433
+ "base_uri": "https://localhost:8080/",
434
+ "height": 204
435
+ },
436
+ "id": "alqXE8F1jpg1",
437
+ "outputId": "ea3e5ffa-e112-4c95-bfcf-97998f3389c6"
438
+ },
439
+ "execution_count": null,
440
+ "outputs": [
441
+ {
442
+ "output_type": "execute_result",
443
+ "data": {
444
+ "text/plain": [
445
+ " title price rating\n",
446
+ "995 Alice in Wonderland (Alice's Adventures in Won... 55.53 One\n",
447
+ "996 Ajin: Demi-Human, Volume 1 (Ajin: Demi-Human #1) 57.06 Four\n",
448
+ "997 A Spy's Devotion (The Regency Spies of London #1) 16.97 Five\n",
449
+ "998 1st to Die (Women's Murder Club #1) 53.98 One\n",
450
+ "999 1,000 Places to See Before You Die 26.08 Five"
451
+ ],
452
+ "text/html": [
453
+ "\n",
454
+ " <div id=\"df-02702c95-696b-4b79-9f9f-ce41c53798b3\" class=\"colab-df-container\">\n",
455
+ " <div>\n",
456
+ "<style scoped>\n",
457
+ " .dataframe tbody tr th:only-of-type {\n",
458
+ " vertical-align: middle;\n",
459
+ " }\n",
460
+ "\n",
461
+ " .dataframe tbody tr th {\n",
462
+ " vertical-align: top;\n",
463
+ " }\n",
464
+ "\n",
465
+ " .dataframe thead th {\n",
466
+ " text-align: right;\n",
467
+ " }\n",
468
+ "</style>\n",
469
+ "<table border=\"1\" class=\"dataframe\">\n",
470
+ " <thead>\n",
471
+ " <tr style=\"text-align: right;\">\n",
472
+ " <th></th>\n",
473
+ " <th>title</th>\n",
474
+ " <th>price</th>\n",
475
+ " <th>rating</th>\n",
476
+ " </tr>\n",
477
+ " </thead>\n",
478
+ " <tbody>\n",
479
+ " <tr>\n",
480
+ " <th>995</th>\n",
481
+ " <td>Alice in Wonderland (Alice's Adventures in Won...</td>\n",
482
+ " <td>55.53</td>\n",
483
+ " <td>One</td>\n",
484
+ " </tr>\n",
485
+ " <tr>\n",
486
+ " <th>996</th>\n",
487
+ " <td>Ajin: Demi-Human, Volume 1 (Ajin: Demi-Human #1)</td>\n",
488
+ " <td>57.06</td>\n",
489
+ " <td>Four</td>\n",
490
+ " </tr>\n",
491
+ " <tr>\n",
492
+ " <th>997</th>\n",
493
+ " <td>A Spy's Devotion (The Regency Spies of London #1)</td>\n",
494
+ " <td>16.97</td>\n",
495
+ " <td>Five</td>\n",
496
+ " </tr>\n",
497
+ " <tr>\n",
498
+ " <th>998</th>\n",
499
+ " <td>1st to Die (Women's Murder Club #1)</td>\n",
500
+ " <td>53.98</td>\n",
501
+ " <td>One</td>\n",
502
+ " </tr>\n",
503
+ " <tr>\n",
504
+ " <th>999</th>\n",
505
+ " <td>1,000 Places to See Before You Die</td>\n",
506
+ " <td>26.08</td>\n",
507
+ " <td>Five</td>\n",
508
+ " </tr>\n",
509
+ " </tbody>\n",
510
+ "</table>\n",
511
+ "</div>\n",
512
+ " <div class=\"colab-df-buttons\">\n",
513
+ "\n",
514
+ " <div class=\"colab-df-container\">\n",
515
+ " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-02702c95-696b-4b79-9f9f-ce41c53798b3')\"\n",
516
+ " title=\"Convert this dataframe to an interactive table.\"\n",
517
+ " style=\"display:none;\">\n",
518
+ "\n",
519
+ " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
520
+ " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
521
+ " </svg>\n",
522
+ " </button>\n",
523
+ "\n",
524
+ " <style>\n",
525
+ " .colab-df-container {\n",
526
+ " display:flex;\n",
527
+ " gap: 12px;\n",
528
+ " }\n",
529
+ "\n",
530
+ " .colab-df-convert {\n",
531
+ " background-color: #E8F0FE;\n",
532
+ " border: none;\n",
533
+ " border-radius: 50%;\n",
534
+ " cursor: pointer;\n",
535
+ " display: none;\n",
536
+ " fill: #1967D2;\n",
537
+ " height: 32px;\n",
538
+ " padding: 0 0 0 0;\n",
539
+ " width: 32px;\n",
540
+ " }\n",
541
+ "\n",
542
+ " .colab-df-convert:hover {\n",
543
+ " background-color: #E2EBFA;\n",
544
+ " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
545
+ " fill: #174EA6;\n",
546
+ " }\n",
547
+ "\n",
548
+ " .colab-df-buttons div {\n",
549
+ " margin-bottom: 4px;\n",
550
+ " }\n",
551
+ "\n",
552
+ " [theme=dark] .colab-df-convert {\n",
553
+ " background-color: #3B4455;\n",
554
+ " fill: #D2E3FC;\n",
555
+ " }\n",
556
+ "\n",
557
+ " [theme=dark] .colab-df-convert:hover {\n",
558
+ " background-color: #434B5C;\n",
559
+ " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
560
+ " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
561
+ " fill: #FFFFFF;\n",
562
+ " }\n",
563
+ " </style>\n",
564
+ "\n",
565
+ " <script>\n",
566
+ " const buttonEl =\n",
567
+ " document.querySelector('#df-02702c95-696b-4b79-9f9f-ce41c53798b3 button.colab-df-convert');\n",
568
+ " buttonEl.style.display =\n",
569
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
570
+ "\n",
571
+ " async function convertToInteractive(key) {\n",
572
+ " const element = document.querySelector('#df-02702c95-696b-4b79-9f9f-ce41c53798b3');\n",
573
+ " const dataTable =\n",
574
+ " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
575
+ " [key], {});\n",
576
+ " if (!dataTable) return;\n",
577
+ "\n",
578
+ " const docLinkHtml = 'Like what you see? Visit the ' +\n",
579
+ " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
580
+ " + ' to learn more about interactive tables.';\n",
581
+ " element.innerHTML = '';\n",
582
+ " dataTable['output_type'] = 'display_data';\n",
583
+ " await google.colab.output.renderOutput(dataTable, element);\n",
584
+ " const docLink = document.createElement('div');\n",
585
+ " docLink.innerHTML = docLinkHtml;\n",
586
+ " element.appendChild(docLink);\n",
587
+ " }\n",
588
+ " </script>\n",
589
+ " </div>\n",
590
+ "\n",
591
+ "\n",
592
+ " </div>\n",
593
+ " </div>\n"
594
+ ],
595
+ "application/vnd.google.colaboratory.intrinsic+json": {
596
+ "type": "dataframe",
597
+ "summary": "{\n \"name\": \"df_books\",\n \"rows\": 5,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 5,\n \"samples\": [\n \"Ajin: Demi-Human, Volume 1 (Ajin: Demi-Human #1)\",\n \"1,000 Places to See Before You Die\",\n \"A Spy's Devotion (The Regency Spies of London #1)\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"price\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 18.92949629546439,\n \"min\": 16.97,\n \"max\": 57.06,\n \"num_unique_values\": 5,\n \"samples\": [\n 57.06,\n 26.08,\n 16.97\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"rating\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"One\",\n \"Four\",\n \"Five\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
598
+ }
599
+ },
600
+ "metadata": {},
601
+ "execution_count": 12
602
+ }
603
+ ]
604
+ },
605
+ {
606
+ "cell_type": "markdown",
607
+ "metadata": {
608
+ "id": "duI5dv3CZYvF"
609
+ },
610
+ "source": [
611
+ "### *d. Save web-scraped dataframe either as a CSV or Excel file*"
612
+ ]
613
+ },
614
+ {
615
+ "cell_type": "code",
616
+ "execution_count": null,
617
+ "metadata": {
618
+ "id": "lC1U_YHtZifh"
619
+ },
620
+ "outputs": [],
621
+ "source": [
622
+ "# 💾 Save to CSV\n",
623
+ "df_books.to_csv(\"books_data.csv\", index=False)\n",
624
+ "\n",
625
+ "# 💾 Or save to Excel\n",
626
+ "# df_books.to_excel(\"books_data.xlsx\", index=False)"
627
+ ]
628
+ },
629
+ {
630
+ "cell_type": "markdown",
631
+ "metadata": {
632
+ "id": "qMjRKMBQZlJi"
633
+ },
634
+ "source": [
635
+ "### *e. ✋🏻🛑⛔️ View first fiew lines*"
636
+ ]
637
+ },
638
+ {
639
+ "cell_type": "code",
640
+ "execution_count": null,
641
+ "metadata": {
642
+ "colab": {
643
+ "base_uri": "https://localhost:8080/",
644
+ "height": 206
645
+ },
646
+ "id": "O_wIvTxYZqCK",
647
+ "outputId": "349b36b0-c008-4fd5-d4a4-dba38ae18337"
648
+ },
649
+ "outputs": [
650
+ {
651
+ "output_type": "execute_result",
652
+ "data": {
653
+ "text/plain": [
654
+ " title price rating\n",
655
+ "0 A Light in the Attic 51.77 Three\n",
656
+ "1 Tipping the Velvet 53.74 One\n",
657
+ "2 Soumission 50.10 One\n",
658
+ "3 Sharp Objects 47.82 Four\n",
659
+ "4 Sapiens: A Brief History of Humankind 54.23 Five"
660
+ ],
661
+ "text/html": [
662
+ "\n",
663
+ " <div id=\"df-04c87660-4415-45e9-ad3b-3fa19d9402c2\" class=\"colab-df-container\">\n",
664
+ " <div>\n",
665
+ "<style scoped>\n",
666
+ " .dataframe tbody tr th:only-of-type {\n",
667
+ " vertical-align: middle;\n",
668
+ " }\n",
669
+ "\n",
670
+ " .dataframe tbody tr th {\n",
671
+ " vertical-align: top;\n",
672
+ " }\n",
673
+ "\n",
674
+ " .dataframe thead th {\n",
675
+ " text-align: right;\n",
676
+ " }\n",
677
+ "</style>\n",
678
+ "<table border=\"1\" class=\"dataframe\">\n",
679
+ " <thead>\n",
680
+ " <tr style=\"text-align: right;\">\n",
681
+ " <th></th>\n",
682
+ " <th>title</th>\n",
683
+ " <th>price</th>\n",
684
+ " <th>rating</th>\n",
685
+ " </tr>\n",
686
+ " </thead>\n",
687
+ " <tbody>\n",
688
+ " <tr>\n",
689
+ " <th>0</th>\n",
690
+ " <td>A Light in the Attic</td>\n",
691
+ " <td>51.77</td>\n",
692
+ " <td>Three</td>\n",
693
+ " </tr>\n",
694
+ " <tr>\n",
695
+ " <th>1</th>\n",
696
+ " <td>Tipping the Velvet</td>\n",
697
+ " <td>53.74</td>\n",
698
+ " <td>One</td>\n",
699
+ " </tr>\n",
700
+ " <tr>\n",
701
+ " <th>2</th>\n",
702
+ " <td>Soumission</td>\n",
703
+ " <td>50.10</td>\n",
704
+ " <td>One</td>\n",
705
+ " </tr>\n",
706
+ " <tr>\n",
707
+ " <th>3</th>\n",
708
+ " <td>Sharp Objects</td>\n",
709
+ " <td>47.82</td>\n",
710
+ " <td>Four</td>\n",
711
+ " </tr>\n",
712
+ " <tr>\n",
713
+ " <th>4</th>\n",
714
+ " <td>Sapiens: A Brief History of Humankind</td>\n",
715
+ " <td>54.23</td>\n",
716
+ " <td>Five</td>\n",
717
+ " </tr>\n",
718
+ " </tbody>\n",
719
+ "</table>\n",
720
+ "</div>\n",
721
+ " <div class=\"colab-df-buttons\">\n",
722
+ "\n",
723
+ " <div class=\"colab-df-container\">\n",
724
+ " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-04c87660-4415-45e9-ad3b-3fa19d9402c2')\"\n",
725
+ " title=\"Convert this dataframe to an interactive table.\"\n",
726
+ " style=\"display:none;\">\n",
727
+ "\n",
728
+ " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
729
+ " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
730
+ " </svg>\n",
731
+ " </button>\n",
732
+ "\n",
733
+ " <style>\n",
734
+ " .colab-df-container {\n",
735
+ " display:flex;\n",
736
+ " gap: 12px;\n",
737
+ " }\n",
738
+ "\n",
739
+ " .colab-df-convert {\n",
740
+ " background-color: #E8F0FE;\n",
741
+ " border: none;\n",
742
+ " border-radius: 50%;\n",
743
+ " cursor: pointer;\n",
744
+ " display: none;\n",
745
+ " fill: #1967D2;\n",
746
+ " height: 32px;\n",
747
+ " padding: 0 0 0 0;\n",
748
+ " width: 32px;\n",
749
+ " }\n",
750
+ "\n",
751
+ " .colab-df-convert:hover {\n",
752
+ " background-color: #E2EBFA;\n",
753
+ " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
754
+ " fill: #174EA6;\n",
755
+ " }\n",
756
+ "\n",
757
+ " .colab-df-buttons div {\n",
758
+ " margin-bottom: 4px;\n",
759
+ " }\n",
760
+ "\n",
761
+ " [theme=dark] .colab-df-convert {\n",
762
+ " background-color: #3B4455;\n",
763
+ " fill: #D2E3FC;\n",
764
+ " }\n",
765
+ "\n",
766
+ " [theme=dark] .colab-df-convert:hover {\n",
767
+ " background-color: #434B5C;\n",
768
+ " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
769
+ " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
770
+ " fill: #FFFFFF;\n",
771
+ " }\n",
772
+ " </style>\n",
773
+ "\n",
774
+ " <script>\n",
775
+ " const buttonEl =\n",
776
+ " document.querySelector('#df-04c87660-4415-45e9-ad3b-3fa19d9402c2 button.colab-df-convert');\n",
777
+ " buttonEl.style.display =\n",
778
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
779
+ "\n",
780
+ " async function convertToInteractive(key) {\n",
781
+ " const element = document.querySelector('#df-04c87660-4415-45e9-ad3b-3fa19d9402c2');\n",
782
+ " const dataTable =\n",
783
+ " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
784
+ " [key], {});\n",
785
+ " if (!dataTable) return;\n",
786
+ "\n",
787
+ " const docLinkHtml = 'Like what you see? Visit the ' +\n",
788
+ " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
789
+ " + ' to learn more about interactive tables.';\n",
790
+ " element.innerHTML = '';\n",
791
+ " dataTable['output_type'] = 'display_data';\n",
792
+ " await google.colab.output.renderOutput(dataTable, element);\n",
793
+ " const docLink = document.createElement('div');\n",
794
+ " docLink.innerHTML = docLinkHtml;\n",
795
+ " element.appendChild(docLink);\n",
796
+ " }\n",
797
+ " </script>\n",
798
+ " </div>\n",
799
+ "\n",
800
+ "\n",
801
+ " </div>\n",
802
+ " </div>\n"
803
+ ],
804
+ "application/vnd.google.colaboratory.intrinsic+json": {
805
+ "type": "dataframe",
806
+ "variable_name": "df_books",
807
+ "summary": "{\n \"name\": \"df_books\",\n \"rows\": 1000,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 999,\n \"samples\": [\n \"The Grownup\",\n \"Persepolis: The Story of a Childhood (Persepolis #1-2)\",\n \"Ayumi's Violin\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"price\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 14.446689669952772,\n \"min\": 10.0,\n \"max\": 59.99,\n \"num_unique_values\": 903,\n \"samples\": [\n 19.73,\n 55.65,\n 46.31\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"rating\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 5,\n \"samples\": [\n \"One\",\n \"Two\",\n \"Four\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
808
+ }
809
+ },
810
+ "metadata": {},
811
+ "execution_count": 6
812
+ }
813
+ ],
814
+ "source": []
815
+ },
816
+ {
817
+ "cell_type": "markdown",
818
+ "metadata": {
819
+ "id": "p-1Pr2szaqLk"
820
+ },
821
+ "source": [
822
+ "## **3.** 🧩 Create a meaningful connection between real & synthetic datasets"
823
+ ]
824
+ },
825
+ {
826
+ "cell_type": "markdown",
827
+ "metadata": {
828
+ "id": "SIaJUGIpaH4V"
829
+ },
830
+ "source": [
831
+ "### *a. Initial setup*"
832
+ ]
833
+ },
834
+ {
835
+ "cell_type": "code",
836
+ "execution_count": null,
837
+ "metadata": {
838
+ "id": "-gPXGcRPuV_9"
839
+ },
840
+ "outputs": [],
841
+ "source": [
842
+ "import numpy as np\n",
843
+ "import random\n",
844
+ "from datetime import datetime\n",
845
+ "import warnings\n",
846
+ "\n",
847
+ "warnings.filterwarnings(\"ignore\")\n",
848
+ "random.seed(2025)\n",
849
+ "np.random.seed(2025)"
850
+ ]
851
+ },
852
+ {
853
+ "cell_type": "markdown",
854
+ "metadata": {
855
+ "id": "pY4yCoIuaQqp"
856
+ },
857
+ "source": [
858
+ "### *b. Generate popularity scores based on rating (with some randomness) with a generate_popularity_score function*"
859
+ ]
860
+ },
861
+ {
862
+ "cell_type": "code",
863
+ "execution_count": null,
864
+ "metadata": {
865
+ "id": "mnd5hdAbaNjz"
866
+ },
867
+ "outputs": [],
868
+ "source": [
869
+ "def generate_popularity_score(rating):\n",
870
+ " base = {\"One\": 2, \"Two\": 3, \"Three\": 3, \"Four\": 4, \"Five\": 4}.get(rating, 3)\n",
871
+ " trend_factor = random.choices([-1, 0, 1], weights=[1, 3, 2])[0]\n",
872
+ " return int(np.clip(base + trend_factor, 1, 5))"
873
+ ]
874
+ },
875
+ {
876
+ "cell_type": "markdown",
877
+ "metadata": {
878
+ "id": "n4-TaNTFgPak"
879
+ },
880
+ "source": [
881
+ "### *c. ✋🏻🛑⛔️ Run the function to create a \"popularity_score\" column from \"rating\"*"
882
+ ]
883
+ },
884
+ {
885
+ "cell_type": "code",
886
+ "execution_count": null,
887
+ "metadata": {
888
+ "id": "V-G3OCUCgR07",
889
+ "colab": {
890
+ "base_uri": "https://localhost:8080/",
891
+ "height": 204
892
+ },
893
+ "outputId": "0f163540-194b-41a3-e4bf-6544088484df"
894
+ },
895
+ "outputs": [
896
+ {
897
+ "output_type": "execute_result",
898
+ "data": {
899
+ "text/plain": [
900
+ " title price rating popularity_score\n",
901
+ "0 A Light in the Attic 51.77 Three 3\n",
902
+ "1 Tipping the Velvet 53.74 One 2\n",
903
+ "2 Soumission 50.10 One 2\n",
904
+ "3 Sharp Objects 47.82 Four 4\n",
905
+ "4 Sapiens: A Brief History of Humankind 54.23 Five 3"
906
+ ],
907
+ "text/html": [
908
+ "\n",
909
+ " <div id=\"df-2ce674ec-7be5-459d-bbbe-999b798153f8\" class=\"colab-df-container\">\n",
910
+ " <div>\n",
911
+ "<style scoped>\n",
912
+ " .dataframe tbody tr th:only-of-type {\n",
913
+ " vertical-align: middle;\n",
914
+ " }\n",
915
+ "\n",
916
+ " .dataframe tbody tr th {\n",
917
+ " vertical-align: top;\n",
918
+ " }\n",
919
+ "\n",
920
+ " .dataframe thead th {\n",
921
+ " text-align: right;\n",
922
+ " }\n",
923
+ "</style>\n",
924
+ "<table border=\"1\" class=\"dataframe\">\n",
925
+ " <thead>\n",
926
+ " <tr style=\"text-align: right;\">\n",
927
+ " <th></th>\n",
928
+ " <th>title</th>\n",
929
+ " <th>price</th>\n",
930
+ " <th>rating</th>\n",
931
+ " <th>popularity_score</th>\n",
932
+ " </tr>\n",
933
+ " </thead>\n",
934
+ " <tbody>\n",
935
+ " <tr>\n",
936
+ " <th>0</th>\n",
937
+ " <td>A Light in the Attic</td>\n",
938
+ " <td>51.77</td>\n",
939
+ " <td>Three</td>\n",
940
+ " <td>3</td>\n",
941
+ " </tr>\n",
942
+ " <tr>\n",
943
+ " <th>1</th>\n",
944
+ " <td>Tipping the Velvet</td>\n",
945
+ " <td>53.74</td>\n",
946
+ " <td>One</td>\n",
947
+ " <td>2</td>\n",
948
+ " </tr>\n",
949
+ " <tr>\n",
950
+ " <th>2</th>\n",
951
+ " <td>Soumission</td>\n",
952
+ " <td>50.10</td>\n",
953
+ " <td>One</td>\n",
954
+ " <td>2</td>\n",
955
+ " </tr>\n",
956
+ " <tr>\n",
957
+ " <th>3</th>\n",
958
+ " <td>Sharp Objects</td>\n",
959
+ " <td>47.82</td>\n",
960
+ " <td>Four</td>\n",
961
+ " <td>4</td>\n",
962
+ " </tr>\n",
963
+ " <tr>\n",
964
+ " <th>4</th>\n",
965
+ " <td>Sapiens: A Brief History of Humankind</td>\n",
966
+ " <td>54.23</td>\n",
967
+ " <td>Five</td>\n",
968
+ " <td>3</td>\n",
969
+ " </tr>\n",
970
+ " </tbody>\n",
971
+ "</table>\n",
972
+ "</div>\n",
973
+ " <div class=\"colab-df-buttons\">\n",
974
+ "\n",
975
+ " <div class=\"colab-df-container\">\n",
976
+ " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-2ce674ec-7be5-459d-bbbe-999b798153f8')\"\n",
977
+ " title=\"Convert this dataframe to an interactive table.\"\n",
978
+ " style=\"display:none;\">\n",
979
+ "\n",
980
+ " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
981
+ " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
982
+ " </svg>\n",
983
+ " </button>\n",
984
+ "\n",
985
+ " <style>\n",
986
+ " .colab-df-container {\n",
987
+ " display:flex;\n",
988
+ " gap: 12px;\n",
989
+ " }\n",
990
+ "\n",
991
+ " .colab-df-convert {\n",
992
+ " background-color: #E8F0FE;\n",
993
+ " border: none;\n",
994
+ " border-radius: 50%;\n",
995
+ " cursor: pointer;\n",
996
+ " display: none;\n",
997
+ " fill: #1967D2;\n",
998
+ " height: 32px;\n",
999
+ " padding: 0 0 0 0;\n",
1000
+ " width: 32px;\n",
1001
+ " }\n",
1002
+ "\n",
1003
+ " .colab-df-convert:hover {\n",
1004
+ " background-color: #E2EBFA;\n",
1005
+ " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
1006
+ " fill: #174EA6;\n",
1007
+ " }\n",
1008
+ "\n",
1009
+ " .colab-df-buttons div {\n",
1010
+ " margin-bottom: 4px;\n",
1011
+ " }\n",
1012
+ "\n",
1013
+ " [theme=dark] .colab-df-convert {\n",
1014
+ " background-color: #3B4455;\n",
1015
+ " fill: #D2E3FC;\n",
1016
+ " }\n",
1017
+ "\n",
1018
+ " [theme=dark] .colab-df-convert:hover {\n",
1019
+ " background-color: #434B5C;\n",
1020
+ " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
1021
+ " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
1022
+ " fill: #FFFFFF;\n",
1023
+ " }\n",
1024
+ " </style>\n",
1025
+ "\n",
1026
+ " <script>\n",
1027
+ " const buttonEl =\n",
1028
+ " document.querySelector('#df-2ce674ec-7be5-459d-bbbe-999b798153f8 button.colab-df-convert');\n",
1029
+ " buttonEl.style.display =\n",
1030
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
1031
+ "\n",
1032
+ " async function convertToInteractive(key) {\n",
1033
+ " const element = document.querySelector('#df-2ce674ec-7be5-459d-bbbe-999b798153f8');\n",
1034
+ " const dataTable =\n",
1035
+ " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
1036
+ " [key], {});\n",
1037
+ " if (!dataTable) return;\n",
1038
+ "\n",
1039
+ " const docLinkHtml = 'Like what you see? Visit the ' +\n",
1040
+ " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
1041
+ " + ' to learn more about interactive tables.';\n",
1042
+ " element.innerHTML = '';\n",
1043
+ " dataTable['output_type'] = 'display_data';\n",
1044
+ " await google.colab.output.renderOutput(dataTable, element);\n",
1045
+ " const docLink = document.createElement('div');\n",
1046
+ " docLink.innerHTML = docLinkHtml;\n",
1047
+ " element.appendChild(docLink);\n",
1048
+ " }\n",
1049
+ " </script>\n",
1050
+ " </div>\n",
1051
+ "\n",
1052
+ "\n",
1053
+ " </div>\n",
1054
+ " </div>\n"
1055
+ ],
1056
+ "application/vnd.google.colaboratory.intrinsic+json": {
1057
+ "type": "dataframe",
1058
+ "variable_name": "df_books",
1059
+ "summary": "{\n \"name\": \"df_books\",\n \"rows\": 1000,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 999,\n \"samples\": [\n \"The Grownup\",\n \"Persepolis: The Story of a Childhood (Persepolis #1-2)\",\n \"Ayumi's Violin\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"price\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 14.446689669952772,\n \"min\": 10.0,\n \"max\": 59.99,\n \"num_unique_values\": 903,\n \"samples\": [\n 19.73,\n 55.65,\n 46.31\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"rating\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 5,\n \"samples\": [\n \"One\",\n \"Two\",\n \"Four\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"popularity_score\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 1,\n \"max\": 5,\n \"num_unique_values\": 5,\n \"samples\": [\n 2,\n 5,\n 4\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
1060
+ }
1061
+ },
1062
+ "metadata": {},
1063
+ "execution_count": 15
1064
+ }
1065
+ ],
1066
+ "source": [
1067
+ "df_books[\"popularity_score\"] = df_books[\"rating\"].apply(generate_popularity_score)\n",
1068
+ "\n",
1069
+ "df_books.head()"
1070
+ ]
1071
+ },
1072
+ {
1073
+ "cell_type": "markdown",
1074
+ "metadata": {
1075
+ "id": "HnngRNTgacYt"
1076
+ },
1077
+ "source": [
1078
+ "### *d. Decide on the sentiment_label based on the popularity score with a get_sentiment function*"
1079
+ ]
1080
+ },
1081
+ {
1082
+ "cell_type": "code",
1083
+ "execution_count": null,
1084
+ "metadata": {
1085
+ "id": "kUtWmr8maZLZ"
1086
+ },
1087
+ "outputs": [],
1088
+ "source": [
1089
+ "def get_sentiment(popularity_score):\n",
1090
+ " if popularity_score <= 2:\n",
1091
+ " return \"negative\"\n",
1092
+ " elif popularity_score == 3:\n",
1093
+ " return \"neutral\"\n",
1094
+ " else:\n",
1095
+ " return \"positive\""
1096
+ ]
1097
+ },
1098
+ {
1099
+ "cell_type": "markdown",
1100
+ "metadata": {
1101
+ "id": "HF9F9HIzgT7Z"
1102
+ },
1103
+ "source": [
1104
+ "### *e. ✋🏻🛑⛔️ Run the function to create a \"sentiment_label\" column from \"popularity_score\"*"
1105
+ ]
1106
+ },
1107
+ {
1108
+ "cell_type": "code",
1109
+ "execution_count": null,
1110
+ "metadata": {
1111
+ "id": "tafQj8_7gYCG",
1112
+ "colab": {
1113
+ "base_uri": "https://localhost:8080/",
1114
+ "height": 204
1115
+ },
1116
+ "outputId": "fbb79831-c1b8-4f2d-e502-79100a8314b4"
1117
+ },
1118
+ "outputs": [
1119
+ {
1120
+ "output_type": "execute_result",
1121
+ "data": {
1122
+ "text/plain": [
1123
+ " title price rating popularity_score \\\n",
1124
+ "0 A Light in the Attic 51.77 Three 3 \n",
1125
+ "1 Tipping the Velvet 53.74 One 2 \n",
1126
+ "2 Soumission 50.10 One 2 \n",
1127
+ "3 Sharp Objects 47.82 Four 4 \n",
1128
+ "4 Sapiens: A Brief History of Humankind 54.23 Five 3 \n",
1129
+ "\n",
1130
+ " sentiment_label \n",
1131
+ "0 neutral \n",
1132
+ "1 negative \n",
1133
+ "2 negative \n",
1134
+ "3 positive \n",
1135
+ "4 neutral "
1136
+ ],
1137
+ "text/html": [
1138
+ "\n",
1139
+ " <div id=\"df-cfa4a00b-2c8d-493f-8c9a-4c9257e34f32\" class=\"colab-df-container\">\n",
1140
+ " <div>\n",
1141
+ "<style scoped>\n",
1142
+ " .dataframe tbody tr th:only-of-type {\n",
1143
+ " vertical-align: middle;\n",
1144
+ " }\n",
1145
+ "\n",
1146
+ " .dataframe tbody tr th {\n",
1147
+ " vertical-align: top;\n",
1148
+ " }\n",
1149
+ "\n",
1150
+ " .dataframe thead th {\n",
1151
+ " text-align: right;\n",
1152
+ " }\n",
1153
+ "</style>\n",
1154
+ "<table border=\"1\" class=\"dataframe\">\n",
1155
+ " <thead>\n",
1156
+ " <tr style=\"text-align: right;\">\n",
1157
+ " <th></th>\n",
1158
+ " <th>title</th>\n",
1159
+ " <th>price</th>\n",
1160
+ " <th>rating</th>\n",
1161
+ " <th>popularity_score</th>\n",
1162
+ " <th>sentiment_label</th>\n",
1163
+ " </tr>\n",
1164
+ " </thead>\n",
1165
+ " <tbody>\n",
1166
+ " <tr>\n",
1167
+ " <th>0</th>\n",
1168
+ " <td>A Light in the Attic</td>\n",
1169
+ " <td>51.77</td>\n",
1170
+ " <td>Three</td>\n",
1171
+ " <td>3</td>\n",
1172
+ " <td>neutral</td>\n",
1173
+ " </tr>\n",
1174
+ " <tr>\n",
1175
+ " <th>1</th>\n",
1176
+ " <td>Tipping the Velvet</td>\n",
1177
+ " <td>53.74</td>\n",
1178
+ " <td>One</td>\n",
1179
+ " <td>2</td>\n",
1180
+ " <td>negative</td>\n",
1181
+ " </tr>\n",
1182
+ " <tr>\n",
1183
+ " <th>2</th>\n",
1184
+ " <td>Soumission</td>\n",
1185
+ " <td>50.10</td>\n",
1186
+ " <td>One</td>\n",
1187
+ " <td>2</td>\n",
1188
+ " <td>negative</td>\n",
1189
+ " </tr>\n",
1190
+ " <tr>\n",
1191
+ " <th>3</th>\n",
1192
+ " <td>Sharp Objects</td>\n",
1193
+ " <td>47.82</td>\n",
1194
+ " <td>Four</td>\n",
1195
+ " <td>4</td>\n",
1196
+ " <td>positive</td>\n",
1197
+ " </tr>\n",
1198
+ " <tr>\n",
1199
+ " <th>4</th>\n",
1200
+ " <td>Sapiens: A Brief History of Humankind</td>\n",
1201
+ " <td>54.23</td>\n",
1202
+ " <td>Five</td>\n",
1203
+ " <td>3</td>\n",
1204
+ " <td>neutral</td>\n",
1205
+ " </tr>\n",
1206
+ " </tbody>\n",
1207
+ "</table>\n",
1208
+ "</div>\n",
1209
+ " <div class=\"colab-df-buttons\">\n",
1210
+ "\n",
1211
+ " <div class=\"colab-df-container\">\n",
1212
+ " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-cfa4a00b-2c8d-493f-8c9a-4c9257e34f32')\"\n",
1213
+ " title=\"Convert this dataframe to an interactive table.\"\n",
1214
+ " style=\"display:none;\">\n",
1215
+ "\n",
1216
+ " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
1217
+ " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
1218
+ " </svg>\n",
1219
+ " </button>\n",
1220
+ "\n",
1221
+ " <style>\n",
1222
+ " .colab-df-container {\n",
1223
+ " display:flex;\n",
1224
+ " gap: 12px;\n",
1225
+ " }\n",
1226
+ "\n",
1227
+ " .colab-df-convert {\n",
1228
+ " background-color: #E8F0FE;\n",
1229
+ " border: none;\n",
1230
+ " border-radius: 50%;\n",
1231
+ " cursor: pointer;\n",
1232
+ " display: none;\n",
1233
+ " fill: #1967D2;\n",
1234
+ " height: 32px;\n",
1235
+ " padding: 0 0 0 0;\n",
1236
+ " width: 32px;\n",
1237
+ " }\n",
1238
+ "\n",
1239
+ " .colab-df-convert:hover {\n",
1240
+ " background-color: #E2EBFA;\n",
1241
+ " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
1242
+ " fill: #174EA6;\n",
1243
+ " }\n",
1244
+ "\n",
1245
+ " .colab-df-buttons div {\n",
1246
+ " margin-bottom: 4px;\n",
1247
+ " }\n",
1248
+ "\n",
1249
+ " [theme=dark] .colab-df-convert {\n",
1250
+ " background-color: #3B4455;\n",
1251
+ " fill: #D2E3FC;\n",
1252
+ " }\n",
1253
+ "\n",
1254
+ " [theme=dark] .colab-df-convert:hover {\n",
1255
+ " background-color: #434B5C;\n",
1256
+ " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
1257
+ " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
1258
+ " fill: #FFFFFF;\n",
1259
+ " }\n",
1260
+ " </style>\n",
1261
+ "\n",
1262
+ " <script>\n",
1263
+ " const buttonEl =\n",
1264
+ " document.querySelector('#df-cfa4a00b-2c8d-493f-8c9a-4c9257e34f32 button.colab-df-convert');\n",
1265
+ " buttonEl.style.display =\n",
1266
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
1267
+ "\n",
1268
+ " async function convertToInteractive(key) {\n",
1269
+ " const element = document.querySelector('#df-cfa4a00b-2c8d-493f-8c9a-4c9257e34f32');\n",
1270
+ " const dataTable =\n",
1271
+ " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
1272
+ " [key], {});\n",
1273
+ " if (!dataTable) return;\n",
1274
+ "\n",
1275
+ " const docLinkHtml = 'Like what you see? Visit the ' +\n",
1276
+ " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
1277
+ " + ' to learn more about interactive tables.';\n",
1278
+ " element.innerHTML = '';\n",
1279
+ " dataTable['output_type'] = 'display_data';\n",
1280
+ " await google.colab.output.renderOutput(dataTable, element);\n",
1281
+ " const docLink = document.createElement('div');\n",
1282
+ " docLink.innerHTML = docLinkHtml;\n",
1283
+ " element.appendChild(docLink);\n",
1284
+ " }\n",
1285
+ " </script>\n",
1286
+ " </div>\n",
1287
+ "\n",
1288
+ "\n",
1289
+ " </div>\n",
1290
+ " </div>\n"
1291
+ ],
1292
+ "application/vnd.google.colaboratory.intrinsic+json": {
1293
+ "type": "dataframe",
1294
+ "variable_name": "df_books",
1295
+ "summary": "{\n \"name\": \"df_books\",\n \"rows\": 1000,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 999,\n \"samples\": [\n \"The Grownup\",\n \"Persepolis: The Story of a Childhood (Persepolis #1-2)\",\n \"Ayumi's Violin\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"price\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 14.446689669952772,\n \"min\": 10.0,\n \"max\": 59.99,\n \"num_unique_values\": 903,\n \"samples\": [\n 19.73,\n 55.65,\n 46.31\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"rating\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 5,\n \"samples\": [\n \"One\",\n \"Two\",\n \"Four\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"popularity_score\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 1,\n \"max\": 5,\n \"num_unique_values\": 5,\n \"samples\": [\n 2,\n 5,\n 4\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"sentiment_label\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"neutral\",\n \"negative\",\n \"positive\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
1296
+ }
1297
+ },
1298
+ "metadata": {},
1299
+ "execution_count": 17
1300
+ }
1301
+ ],
1302
+ "source": [
1303
+ "df_books[\"sentiment_label\"] = df_books[\"popularity_score\"].apply(get_sentiment)\n",
1304
+ "\n",
1305
+ "df_books.head()"
1306
+ ]
1307
+ },
1308
+ {
1309
+ "cell_type": "markdown",
1310
+ "metadata": {
1311
+ "id": "T8AdKkmASq9a"
1312
+ },
1313
+ "source": [
1314
+ "## **4.** 📈 Generate synthetic book sales data of 18 months"
1315
+ ]
1316
+ },
1317
+ {
1318
+ "cell_type": "markdown",
1319
+ "metadata": {
1320
+ "id": "OhXbdGD5fH0c"
1321
+ },
1322
+ "source": [
1323
+ "### *a. Create a generate_sales_profit function that would generate sales patterns based on sentiment_label (with some randomness)*"
1324
+ ]
1325
+ },
1326
+ {
1327
+ "cell_type": "code",
1328
+ "execution_count": null,
1329
+ "metadata": {
1330
+ "id": "qkVhYPXGbgEn"
1331
+ },
1332
+ "outputs": [],
1333
+ "source": [
1334
+ "def generate_sales_profile(sentiment):\n",
1335
+ " months = pd.date_range(end=datetime.today(), periods=18, freq=\"M\")\n",
1336
+ "\n",
1337
+ " if sentiment == \"positive\":\n",
1338
+ " base = random.randint(200, 300)\n",
1339
+ " trend = np.linspace(base, base + random.randint(20, 60), len(months))\n",
1340
+ " elif sentiment == \"negative\":\n",
1341
+ " base = random.randint(20, 80)\n",
1342
+ " trend = np.linspace(base, base - random.randint(10, 30), len(months))\n",
1343
+ " else: # neutral\n",
1344
+ " base = random.randint(80, 160)\n",
1345
+ " trend = np.full(len(months), base + random.randint(-10, 10))\n",
1346
+ "\n",
1347
+ " seasonality = 10 * np.sin(np.linspace(0, 3 * np.pi, len(months)))\n",
1348
+ " noise = np.random.normal(0, 5, len(months))\n",
1349
+ " monthly_sales = np.clip(trend + seasonality + noise, a_min=0, a_max=None).astype(int)\n",
1350
+ "\n",
1351
+ " return list(zip(months.strftime(\"%Y-%m\"), monthly_sales))"
1352
+ ]
1353
+ },
1354
+ {
1355
+ "cell_type": "markdown",
1356
+ "metadata": {
1357
+ "id": "L2ak1HlcgoTe"
1358
+ },
1359
+ "source": [
1360
+ "### *b. Run the function as part of building sales_data*"
1361
+ ]
1362
+ },
1363
+ {
1364
+ "cell_type": "code",
1365
+ "execution_count": null,
1366
+ "metadata": {
1367
+ "id": "SlJ24AUafoDB"
1368
+ },
1369
+ "outputs": [],
1370
+ "source": [
1371
+ "sales_data = []\n",
1372
+ "for _, row in df_books.iterrows():\n",
1373
+ " records = generate_sales_profile(row[\"sentiment_label\"])\n",
1374
+ " for month, units in records:\n",
1375
+ " sales_data.append({\n",
1376
+ " \"title\": row[\"title\"],\n",
1377
+ " \"month\": month,\n",
1378
+ " \"units_sold\": units,\n",
1379
+ " \"sentiment_label\": row[\"sentiment_label\"]\n",
1380
+ " })"
1381
+ ]
1382
+ },
1383
+ {
1384
+ "cell_type": "markdown",
1385
+ "metadata": {
1386
+ "id": "4IXZKcCSgxnq"
1387
+ },
1388
+ "source": [
1389
+ "### *c. ✋🏻🛑⛔️ Create a df_sales DataFrame from sales_data*"
1390
+ ]
1391
+ },
1392
+ {
1393
+ "cell_type": "code",
1394
+ "execution_count": null,
1395
+ "metadata": {
1396
+ "id": "wcN6gtiZg-ws",
1397
+ "colab": {
1398
+ "base_uri": "https://localhost:8080/",
1399
+ "height": 204
1400
+ },
1401
+ "outputId": "0923c679-8e5d-4b12-889f-181996aa4973"
1402
+ },
1403
+ "outputs": [
1404
+ {
1405
+ "output_type": "execute_result",
1406
+ "data": {
1407
+ "text/plain": [
1408
+ " title month units_sold sentiment_label\n",
1409
+ "0 A Light in the Attic 2024-09 100 neutral\n",
1410
+ "1 A Light in the Attic 2024-10 109 neutral\n",
1411
+ "2 A Light in the Attic 2024-11 102 neutral\n",
1412
+ "3 A Light in the Attic 2024-12 107 neutral\n",
1413
+ "4 A Light in the Attic 2025-01 108 neutral"
1414
+ ],
1415
+ "text/html": [
1416
+ "\n",
1417
+ " <div id=\"df-5f75e291-9b18-4cf5-ba33-41b2b2828841\" class=\"colab-df-container\">\n",
1418
+ " <div>\n",
1419
+ "<style scoped>\n",
1420
+ " .dataframe tbody tr th:only-of-type {\n",
1421
+ " vertical-align: middle;\n",
1422
+ " }\n",
1423
+ "\n",
1424
+ " .dataframe tbody tr th {\n",
1425
+ " vertical-align: top;\n",
1426
+ " }\n",
1427
+ "\n",
1428
+ " .dataframe thead th {\n",
1429
+ " text-align: right;\n",
1430
+ " }\n",
1431
+ "</style>\n",
1432
+ "<table border=\"1\" class=\"dataframe\">\n",
1433
+ " <thead>\n",
1434
+ " <tr style=\"text-align: right;\">\n",
1435
+ " <th></th>\n",
1436
+ " <th>title</th>\n",
1437
+ " <th>month</th>\n",
1438
+ " <th>units_sold</th>\n",
1439
+ " <th>sentiment_label</th>\n",
1440
+ " </tr>\n",
1441
+ " </thead>\n",
1442
+ " <tbody>\n",
1443
+ " <tr>\n",
1444
+ " <th>0</th>\n",
1445
+ " <td>A Light in the Attic</td>\n",
1446
+ " <td>2024-09</td>\n",
1447
+ " <td>100</td>\n",
1448
+ " <td>neutral</td>\n",
1449
+ " </tr>\n",
1450
+ " <tr>\n",
1451
+ " <th>1</th>\n",
1452
+ " <td>A Light in the Attic</td>\n",
1453
+ " <td>2024-10</td>\n",
1454
+ " <td>109</td>\n",
1455
+ " <td>neutral</td>\n",
1456
+ " </tr>\n",
1457
+ " <tr>\n",
1458
+ " <th>2</th>\n",
1459
+ " <td>A Light in the Attic</td>\n",
1460
+ " <td>2024-11</td>\n",
1461
+ " <td>102</td>\n",
1462
+ " <td>neutral</td>\n",
1463
+ " </tr>\n",
1464
+ " <tr>\n",
1465
+ " <th>3</th>\n",
1466
+ " <td>A Light in the Attic</td>\n",
1467
+ " <td>2024-12</td>\n",
1468
+ " <td>107</td>\n",
1469
+ " <td>neutral</td>\n",
1470
+ " </tr>\n",
1471
+ " <tr>\n",
1472
+ " <th>4</th>\n",
1473
+ " <td>A Light in the Attic</td>\n",
1474
+ " <td>2025-01</td>\n",
1475
+ " <td>108</td>\n",
1476
+ " <td>neutral</td>\n",
1477
+ " </tr>\n",
1478
+ " </tbody>\n",
1479
+ "</table>\n",
1480
+ "</div>\n",
1481
+ " <div class=\"colab-df-buttons\">\n",
1482
+ "\n",
1483
+ " <div class=\"colab-df-container\">\n",
1484
+ " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-5f75e291-9b18-4cf5-ba33-41b2b2828841')\"\n",
1485
+ " title=\"Convert this dataframe to an interactive table.\"\n",
1486
+ " style=\"display:none;\">\n",
1487
+ "\n",
1488
+ " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
1489
+ " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
1490
+ " </svg>\n",
1491
+ " </button>\n",
1492
+ "\n",
1493
+ " <style>\n",
1494
+ " .colab-df-container {\n",
1495
+ " display:flex;\n",
1496
+ " gap: 12px;\n",
1497
+ " }\n",
1498
+ "\n",
1499
+ " .colab-df-convert {\n",
1500
+ " background-color: #E8F0FE;\n",
1501
+ " border: none;\n",
1502
+ " border-radius: 50%;\n",
1503
+ " cursor: pointer;\n",
1504
+ " display: none;\n",
1505
+ " fill: #1967D2;\n",
1506
+ " height: 32px;\n",
1507
+ " padding: 0 0 0 0;\n",
1508
+ " width: 32px;\n",
1509
+ " }\n",
1510
+ "\n",
1511
+ " .colab-df-convert:hover {\n",
1512
+ " background-color: #E2EBFA;\n",
1513
+ " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
1514
+ " fill: #174EA6;\n",
1515
+ " }\n",
1516
+ "\n",
1517
+ " .colab-df-buttons div {\n",
1518
+ " margin-bottom: 4px;\n",
1519
+ " }\n",
1520
+ "\n",
1521
+ " [theme=dark] .colab-df-convert {\n",
1522
+ " background-color: #3B4455;\n",
1523
+ " fill: #D2E3FC;\n",
1524
+ " }\n",
1525
+ "\n",
1526
+ " [theme=dark] .colab-df-convert:hover {\n",
1527
+ " background-color: #434B5C;\n",
1528
+ " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
1529
+ " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
1530
+ " fill: #FFFFFF;\n",
1531
+ " }\n",
1532
+ " </style>\n",
1533
+ "\n",
1534
+ " <script>\n",
1535
+ " const buttonEl =\n",
1536
+ " document.querySelector('#df-5f75e291-9b18-4cf5-ba33-41b2b2828841 button.colab-df-convert');\n",
1537
+ " buttonEl.style.display =\n",
1538
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
1539
+ "\n",
1540
+ " async function convertToInteractive(key) {\n",
1541
+ " const element = document.querySelector('#df-5f75e291-9b18-4cf5-ba33-41b2b2828841');\n",
1542
+ " const dataTable =\n",
1543
+ " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
1544
+ " [key], {});\n",
1545
+ " if (!dataTable) return;\n",
1546
+ "\n",
1547
+ " const docLinkHtml = 'Like what you see? Visit the ' +\n",
1548
+ " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
1549
+ " + ' to learn more about interactive tables.';\n",
1550
+ " element.innerHTML = '';\n",
1551
+ " dataTable['output_type'] = 'display_data';\n",
1552
+ " await google.colab.output.renderOutput(dataTable, element);\n",
1553
+ " const docLink = document.createElement('div');\n",
1554
+ " docLink.innerHTML = docLinkHtml;\n",
1555
+ " element.appendChild(docLink);\n",
1556
+ " }\n",
1557
+ " </script>\n",
1558
+ " </div>\n",
1559
+ "\n",
1560
+ "\n",
1561
+ " </div>\n",
1562
+ " </div>\n"
1563
+ ],
1564
+ "application/vnd.google.colaboratory.intrinsic+json": {
1565
+ "type": "dataframe",
1566
+ "variable_name": "df_sales",
1567
+ "summary": "{\n \"name\": \"df_sales\",\n \"rows\": 18000,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 999,\n \"samples\": [\n \"The Grownup\",\n \"Persepolis: The Story of a Childhood (Persepolis #1-2)\",\n \"Ayumi's Violin\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"month\",\n \"properties\": {\n \"dtype\": \"object\",\n \"num_unique_values\": 18,\n \"samples\": [\n \"2024-09\",\n \"2024-10\",\n \"2025-05\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"units_sold\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 98,\n \"min\": 0,\n \"max\": 362,\n \"num_unique_values\": 354,\n \"samples\": [\n 214,\n 289,\n 205\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"sentiment_label\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"neutral\",\n \"negative\",\n \"positive\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
1568
+ }
1569
+ },
1570
+ "metadata": {},
1571
+ "execution_count": 20
1572
+ }
1573
+ ],
1574
+ "source": [
1575
+ "df_sales = pd.DataFrame(sales_data)\n",
1576
+ "\n",
1577
+ "df_sales.head()"
1578
+ ]
1579
+ },
1580
+ {
1581
+ "cell_type": "markdown",
1582
+ "metadata": {
1583
+ "id": "EhIjz9WohAmZ"
1584
+ },
1585
+ "source": [
1586
+ "### *d. Save df_sales as synthetic_sales_data.csv & view first few lines*"
1587
+ ]
1588
+ },
1589
+ {
1590
+ "cell_type": "code",
1591
+ "execution_count": null,
1592
+ "metadata": {
1593
+ "colab": {
1594
+ "base_uri": "https://localhost:8080/"
1595
+ },
1596
+ "id": "MzbZvLcAhGaH",
1597
+ "outputId": "a99b62dd-10e6-4ade-d482-61807d61559c"
1598
+ },
1599
+ "outputs": [
1600
+ {
1601
+ "output_type": "stream",
1602
+ "name": "stdout",
1603
+ "text": [
1604
+ " title month units_sold sentiment_label\n",
1605
+ "0 A Light in the Attic 2024-09 100 neutral\n",
1606
+ "1 A Light in the Attic 2024-10 109 neutral\n",
1607
+ "2 A Light in the Attic 2024-11 102 neutral\n",
1608
+ "3 A Light in the Attic 2024-12 107 neutral\n",
1609
+ "4 A Light in the Attic 2025-01 108 neutral\n"
1610
+ ]
1611
+ }
1612
+ ],
1613
+ "source": [
1614
+ "df_sales.to_csv(\"synthetic_sales_data.csv\", index=False)\n",
1615
+ "\n",
1616
+ "print(df_sales.head())"
1617
+ ]
1618
+ },
1619
+ {
1620
+ "cell_type": "markdown",
1621
+ "metadata": {
1622
+ "id": "7g9gqBgQMtJn"
1623
+ },
1624
+ "source": [
1625
+ "## **5.** 🎯 Generate synthetic customer reviews"
1626
+ ]
1627
+ },
1628
+ {
1629
+ "cell_type": "markdown",
1630
+ "metadata": {
1631
+ "id": "Gi4y9M9KuDWx"
1632
+ },
1633
+ "source": [
1634
+ "### *a. ✋🏻🛑⛔️ Ask ChatGPT to create a list of 50 distinct generic book review texts for the sentiment labels \"positive\", \"neutral\", and \"negative\" called synthetic_reviews_by_sentiment*"
1635
+ ]
1636
+ },
1637
+ {
1638
+ "cell_type": "code",
1639
+ "execution_count": null,
1640
+ "metadata": {
1641
+ "id": "b3cd2a50"
1642
+ },
1643
+ "outputs": [],
1644
+ "source": [
1645
+ "synthetic_reviews_by_sentiment = {\n",
1646
+ "\n",
1647
+ " \"positive\": [\n",
1648
+ " \"An unforgettable story that kept me hooked until the last page.\",\n",
1649
+ " \"Beautifully written and deeply moving.\",\n",
1650
+ " \"A masterpiece with rich characters and a gripping plot.\",\n",
1651
+ " \"Absolutely loved every chapter of this book.\",\n",
1652
+ " \"The storytelling was immersive and emotionally powerful.\",\n",
1653
+ " \"A brilliant read that exceeded my expectations.\",\n",
1654
+ " \"Engaging from start to finish.\",\n",
1655
+ " \"The author crafted a truly inspiring narrative.\",\n",
1656
+ " \"One of the most satisfying reads this year.\",\n",
1657
+ " \"A compelling journey filled with heart and depth.\",\n",
1658
+ " \"The characters felt real and relatable.\",\n",
1659
+ " \"An uplifting and rewarding experience.\",\n",
1660
+ " \"The pacing and development were spot on.\",\n",
1661
+ " \"A thoughtful and beautifully executed story.\",\n",
1662
+ " \"Highly entertaining and well-structured.\",\n",
1663
+ " \"An emotionally resonant and memorable read.\",\n",
1664
+ " \"A book I would happily recommend to anyone.\",\n",
1665
+ " \"The plot twists were clever and exciting.\",\n",
1666
+ " \"An outstanding example of great storytelling.\",\n",
1667
+ " \"Rich themes explored with sensitivity and care.\",\n",
1668
+ " \"The dialogue felt natural and impactful.\",\n",
1669
+ " \"A captivating story that stayed with me.\",\n",
1670
+ " \"Strong writing and a satisfying conclusion.\",\n",
1671
+ " \"An impressive and engaging narrative.\",\n",
1672
+ " \"The emotional depth was remarkable.\",\n",
1673
+ " \"A well-crafted and immersive experience.\",\n",
1674
+ " \"The story flowed seamlessly.\",\n",
1675
+ " \"An inspiring and meaningful book.\",\n",
1676
+ " \"Exceptionally well-written.\",\n",
1677
+ " \"A delightful and heartwarming read.\",\n",
1678
+ " \"The characters were unforgettable.\",\n",
1679
+ " \"An exciting and beautifully told story.\",\n",
1680
+ " \"A powerful and moving journey.\",\n",
1681
+ " \"Highly engaging and emotionally rich.\",\n",
1682
+ " \"The world-building was fantastic.\",\n",
1683
+ " \"A rewarding and thoughtful book.\",\n",
1684
+ " \"Creative, compelling, and inspiring.\",\n",
1685
+ " \"A truly enjoyable experience.\",\n",
1686
+ " \"One of my favorite reads lately.\",\n",
1687
+ " \"A strong and satisfying plot.\",\n",
1688
+ " \"Expertly written and deeply engaging.\",\n",
1689
+ " \"An absorbing and powerful story.\",\n",
1690
+ " \"The narrative was captivating throughout.\",\n",
1691
+ " \"A standout book in its genre.\",\n",
1692
+ " \"Brilliant pacing and development.\",\n",
1693
+ " \"An emotionally fulfilling read.\",\n",
1694
+ " \"A deeply engaging storyline.\",\n",
1695
+ " \"A fresh and exciting perspective.\",\n",
1696
+ " \"Compelling and beautifully executed.\",\n",
1697
+ " \"An excellent and inspiring book.\"\n",
1698
+ " ],\n",
1699
+ "\n",
1700
+ " \"neutral\": [\n",
1701
+ " \"An average read overall.\",\n",
1702
+ " \"It was fine but nothing extraordinary.\",\n",
1703
+ " \"Some parts were interesting, others less so.\",\n",
1704
+ " \"A decent way to spend the time.\",\n",
1705
+ " \"Neither great nor terrible.\",\n",
1706
+ " \"The story had some strong moments.\",\n",
1707
+ " \"It met my expectations but did not exceed them.\",\n",
1708
+ " \"An okay book with mixed elements.\",\n",
1709
+ " \"Some chapters stood out more than others.\",\n",
1710
+ " \"A fairly standard storyline.\",\n",
1711
+ " \"It was readable but not remarkable.\",\n",
1712
+ " \"A moderate and balanced experience.\",\n",
1713
+ " \"The pacing felt uneven at times.\",\n",
1714
+ " \"A reasonable read for the genre.\",\n",
1715
+ " \"The characters were somewhat engaging.\",\n",
1716
+ " \"Not bad, but not particularly memorable.\",\n",
1717
+ " \"A straightforward narrative.\",\n",
1718
+ " \"It had potential but felt average.\",\n",
1719
+ " \"The ending was acceptable.\",\n",
1720
+ " \"An ordinary reading experience.\",\n",
1721
+ " \"It held my attention in parts.\",\n",
1722
+ " \"The writing was serviceable.\",\n",
1723
+ " \"A mildly enjoyable book.\",\n",
1724
+ " \"The themes were explored adequately.\",\n",
1725
+ " \"Some predictable plot elements.\",\n",
1726
+ " \"An acceptable but simple story.\",\n",
1727
+ " \"It was fine for casual reading.\",\n",
1728
+ " \"Nothing especially surprising.\",\n",
1729
+ " \"A middle-of-the-road experience.\",\n",
1730
+ " \"The plot was easy to follow.\",\n",
1731
+ " \"A safe and familiar story.\",\n",
1732
+ " \"It had both strengths and weaknesses.\",\n",
1733
+ " \"An average addition to the genre.\",\n",
1734
+ " \"Readable but not outstanding.\",\n",
1735
+ " \"Somewhat engaging throughout.\",\n",
1736
+ " \"It did not leave a strong impression.\",\n",
1737
+ " \"A balanced but unremarkable read.\",\n",
1738
+ " \"The writing was consistent.\",\n",
1739
+ " \"A competent but simple narrative.\",\n",
1740
+ " \"An okay book overall.\",\n",
1741
+ " \"Moderately entertaining.\",\n",
1742
+ " \"It had a few standout moments.\",\n",
1743
+ " \"Neither disappointing nor impressive.\",\n",
1744
+ " \"A fairly typical storyline.\",\n",
1745
+ " \"A predictable but decent read.\",\n",
1746
+ " \"The characters were adequate.\",\n",
1747
+ " \"It was fine for what it was.\",\n",
1748
+ " \"Some good ideas, average execution.\",\n",
1749
+ " \"An acceptable reading experience.\",\n",
1750
+ " \"Overall, just okay.\"\n",
1751
+ " ],\n",
1752
+ "\n",
1753
+ " \"negative\": [\n",
1754
+ " \"I struggled to stay engaged.\",\n",
1755
+ " \"The plot felt confusing and disjointed.\",\n",
1756
+ " \"The characters lacked depth.\",\n",
1757
+ " \"It did not meet my expectations.\",\n",
1758
+ " \"The pacing was slow and uneven.\",\n",
1759
+ " \"I found it difficult to finish.\",\n",
1760
+ " \"The story felt underdeveloped.\",\n",
1761
+ " \"Disappointing overall.\",\n",
1762
+ " \"The writing style did not work for me.\",\n",
1763
+ " \"It lacked emotional impact.\",\n",
1764
+ " \"The plot twists felt forced.\",\n",
1765
+ " \"I expected much more from this book.\",\n",
1766
+ " \"The narrative felt flat.\",\n",
1767
+ " \"The characters were forgettable.\",\n",
1768
+ " \"It was hard to stay interested.\",\n",
1769
+ " \"The ending was unsatisfying.\",\n",
1770
+ " \"The dialogue felt unnatural.\",\n",
1771
+ " \"The story lacked direction.\",\n",
1772
+ " \"Not an enjoyable experience.\",\n",
1773
+ " \"It failed to capture my attention.\",\n",
1774
+ " \"The themes were poorly explored.\",\n",
1775
+ " \"The execution felt weak.\",\n",
1776
+ " \"A frustrating read.\",\n",
1777
+ " \"The structure felt messy.\",\n",
1778
+ " \"I would not recommend this book.\",\n",
1779
+ " \"It felt rushed and incomplete.\",\n",
1780
+ " \"The story lacked coherence.\",\n",
1781
+ " \"The pacing dragged too much.\",\n",
1782
+ " \"The character development was weak.\",\n",
1783
+ " \"It was difficult to connect with the story.\",\n",
1784
+ " \"The narrative felt repetitive.\",\n",
1785
+ " \"An underwhelming experience.\",\n",
1786
+ " \"The writing lacked polish.\",\n",
1787
+ " \"It did not deliver on its promise.\",\n",
1788
+ " \"The plot felt predictable and dull.\",\n",
1789
+ " \"I lost interest halfway through.\",\n",
1790
+ " \"The book felt overly long.\",\n",
1791
+ " \"The storyline was disappointing.\",\n",
1792
+ " \"The tension never built properly.\",\n",
1793
+ " \"It felt uninspired.\",\n",
1794
+ " \"The emotional depth was missing.\",\n",
1795
+ " \"A forgettable and weak read.\",\n",
1796
+ " \"The story lacked originality.\",\n",
1797
+ " \"It was not worth the time.\",\n",
1798
+ " \"The characters felt shallow.\",\n",
1799
+ " \"The writing was inconsistent.\",\n",
1800
+ " \"The conclusion was abrupt.\",\n",
1801
+ " \"The plot was hard to follow.\",\n",
1802
+ " \"Overall, a disappointing book.\",\n",
1803
+ " \"It simply did not work for me.\"\n",
1804
+ " ]\n",
1805
+ "}"
1806
+ ]
1807
+ },
1808
+ {
1809
+ "cell_type": "markdown",
1810
+ "metadata": {
1811
+ "id": "fQhfVaDmuULT"
1812
+ },
1813
+ "source": [
1814
+ "### *b. Generate 10 reviews per book using random sampling from the corresponding 50*"
1815
+ ]
1816
+ },
1817
+ {
1818
+ "cell_type": "code",
1819
+ "execution_count": null,
1820
+ "metadata": {
1821
+ "id": "l2SRc3PjuTGM"
1822
+ },
1823
+ "outputs": [],
1824
+ "source": [
1825
+ "review_rows = []\n",
1826
+ "for _, row in df_books.iterrows():\n",
1827
+ " title = row['title']\n",
1828
+ " sentiment_label = row['sentiment_label']\n",
1829
+ " review_pool = synthetic_reviews_by_sentiment[sentiment_label]\n",
1830
+ " sampled_reviews = random.sample(review_pool, 10)\n",
1831
+ " for review_text in sampled_reviews:\n",
1832
+ " review_rows.append({\n",
1833
+ " \"title\": title,\n",
1834
+ " \"sentiment_label\": sentiment_label,\n",
1835
+ " \"review_text\": review_text,\n",
1836
+ " \"rating\": row['rating'],\n",
1837
+ " \"popularity_score\": row['popularity_score']\n",
1838
+ " })"
1839
+ ]
1840
+ },
1841
+ {
1842
+ "cell_type": "markdown",
1843
+ "metadata": {
1844
+ "id": "bmJMXF-Bukdm"
1845
+ },
1846
+ "source": [
1847
+ "### *c. Create the final dataframe df_reviews & save it as synthetic_book_reviews.csv*"
1848
+ ]
1849
+ },
1850
+ {
1851
+ "cell_type": "code",
1852
+ "execution_count": null,
1853
+ "metadata": {
1854
+ "id": "ZUKUqZsuumsp"
1855
+ },
1856
+ "outputs": [],
1857
+ "source": [
1858
+ "df_reviews = pd.DataFrame(review_rows)\n",
1859
+ "df_reviews.to_csv(\"synthetic_book_reviews.csv\", index=False)"
1860
+ ]
1861
+ },
1862
+ {
1863
+ "cell_type": "markdown",
1864
+ "source": [
1865
+ "### *c. inputs for R*"
1866
+ ],
1867
+ "metadata": {
1868
+ "id": "_602pYUS3gY5"
1869
+ }
1870
+ },
1871
+ {
1872
+ "cell_type": "code",
1873
+ "execution_count": null,
1874
+ "metadata": {
1875
+ "colab": {
1876
+ "base_uri": "https://localhost:8080/"
1877
+ },
1878
+ "id": "3946e521",
1879
+ "outputId": "224addb0-6f3c-4e83-b7b6-ae9213486099"
1880
+ },
1881
+ "outputs": [
1882
+ {
1883
+ "output_type": "stream",
1884
+ "name": "stdout",
1885
+ "text": [
1886
+ "✅ Wrote synthetic_title_level_features.csv\n",
1887
+ "✅ Wrote synthetic_monthly_revenue_series.csv\n"
1888
+ ]
1889
+ }
1890
+ ],
1891
+ "source": [
1892
+ "import numpy as np\n",
1893
+ "\n",
1894
+ "def _safe_num(s):\n",
1895
+ " return pd.to_numeric(\n",
1896
+ " pd.Series(s).astype(str).str.replace(r\"[^0-9.]\", \"\", regex=True),\n",
1897
+ " errors=\"coerce\"\n",
1898
+ " )\n",
1899
+ "\n",
1900
+ "# --- Clean book metadata (price/rating) ---\n",
1901
+ "df_books_r = df_books.copy()\n",
1902
+ "if \"price\" in df_books_r.columns:\n",
1903
+ " df_books_r[\"price\"] = _safe_num(df_books_r[\"price\"])\n",
1904
+ "if \"rating\" in df_books_r.columns:\n",
1905
+ " df_books_r[\"rating\"] = _safe_num(df_books_r[\"rating\"])\n",
1906
+ "\n",
1907
+ "df_books_r[\"title\"] = df_books_r[\"title\"].astype(str).str.strip()\n",
1908
+ "\n",
1909
+ "# --- Clean sales ---\n",
1910
+ "df_sales_r = df_sales.copy()\n",
1911
+ "df_sales_r[\"title\"] = df_sales_r[\"title\"].astype(str).str.strip()\n",
1912
+ "df_sales_r[\"month\"] = pd.to_datetime(df_sales_r[\"month\"], errors=\"coerce\")\n",
1913
+ "df_sales_r[\"units_sold\"] = _safe_num(df_sales_r[\"units_sold\"])\n",
1914
+ "\n",
1915
+ "# --- Clean reviews ---\n",
1916
+ "df_reviews_r = df_reviews.copy()\n",
1917
+ "df_reviews_r[\"title\"] = df_reviews_r[\"title\"].astype(str).str.strip()\n",
1918
+ "df_reviews_r[\"sentiment_label\"] = df_reviews_r[\"sentiment_label\"].astype(str).str.lower().str.strip()\n",
1919
+ "if \"rating\" in df_reviews_r.columns:\n",
1920
+ " df_reviews_r[\"rating\"] = _safe_num(df_reviews_r[\"rating\"])\n",
1921
+ "if \"popularity_score\" in df_reviews_r.columns:\n",
1922
+ " df_reviews_r[\"popularity_score\"] = _safe_num(df_reviews_r[\"popularity_score\"])\n",
1923
+ "\n",
1924
+ "# --- Sentiment shares per title (from reviews) ---\n",
1925
+ "sent_counts = (\n",
1926
+ " df_reviews_r.groupby([\"title\", \"sentiment_label\"])\n",
1927
+ " .size()\n",
1928
+ " .unstack(fill_value=0)\n",
1929
+ ")\n",
1930
+ "for lab in [\"positive\", \"neutral\", \"negative\"]:\n",
1931
+ " if lab not in sent_counts.columns:\n",
1932
+ " sent_counts[lab] = 0\n",
1933
+ "\n",
1934
+ "sent_counts[\"total_reviews\"] = sent_counts[[\"positive\", \"neutral\", \"negative\"]].sum(axis=1)\n",
1935
+ "den = sent_counts[\"total_reviews\"].replace(0, np.nan)\n",
1936
+ "sent_counts[\"share_positive\"] = sent_counts[\"positive\"] / den\n",
1937
+ "sent_counts[\"share_neutral\"] = sent_counts[\"neutral\"] / den\n",
1938
+ "sent_counts[\"share_negative\"] = sent_counts[\"negative\"] / den\n",
1939
+ "sent_counts = sent_counts.reset_index()\n",
1940
+ "\n",
1941
+ "# --- Sales aggregation per title ---\n",
1942
+ "sales_by_title = (\n",
1943
+ " df_sales_r.dropna(subset=[\"title\"])\n",
1944
+ " .groupby(\"title\", as_index=False)\n",
1945
+ " .agg(\n",
1946
+ " months_observed=(\"month\", \"nunique\"),\n",
1947
+ " avg_units_sold=(\"units_sold\", \"mean\"),\n",
1948
+ " total_units_sold=(\"units_sold\", \"sum\"),\n",
1949
+ " )\n",
1950
+ ")\n",
1951
+ "\n",
1952
+ "# --- Title-level features (join sales + books + sentiment) ---\n",
1953
+ "df_title = (\n",
1954
+ " sales_by_title\n",
1955
+ " .merge(df_books_r[[\"title\", \"price\", \"rating\"]], on=\"title\", how=\"left\")\n",
1956
+ " .merge(sent_counts[[\"title\", \"share_positive\", \"share_neutral\", \"share_negative\", \"total_reviews\"]],\n",
1957
+ " on=\"title\", how=\"left\")\n",
1958
+ ")\n",
1959
+ "\n",
1960
+ "df_title[\"avg_revenue\"] = df_title[\"avg_units_sold\"] * df_title[\"price\"]\n",
1961
+ "df_title[\"total_revenue\"] = df_title[\"total_units_sold\"] * df_title[\"price\"]\n",
1962
+ "\n",
1963
+ "df_title.to_csv(\"synthetic_title_level_features.csv\", index=False)\n",
1964
+ "print(\"✅ Wrote synthetic_title_level_features.csv\")\n",
1965
+ "\n",
1966
+ "# --- Monthly revenue series (proxy: units_sold * price) ---\n",
1967
+ "monthly_rev = (\n",
1968
+ " df_sales_r.merge(df_books_r[[\"title\", \"price\"]], on=\"title\", how=\"left\")\n",
1969
+ ")\n",
1970
+ "monthly_rev[\"revenue\"] = monthly_rev[\"units_sold\"] * monthly_rev[\"price\"]\n",
1971
+ "\n",
1972
+ "df_monthly = (\n",
1973
+ " monthly_rev.dropna(subset=[\"month\"])\n",
1974
+ " .groupby(\"month\", as_index=False)[\"revenue\"]\n",
1975
+ " .sum()\n",
1976
+ " .rename(columns={\"revenue\": \"total_revenue\"})\n",
1977
+ " .sort_values(\"month\")\n",
1978
+ ")\n",
1979
+ "# if revenue is all NA (e.g., missing price), fallback to units_sold as a teaching proxy\n",
1980
+ "if df_monthly[\"total_revenue\"].notna().sum() == 0:\n",
1981
+ " df_monthly = (\n",
1982
+ " df_sales_r.dropna(subset=[\"month\"])\n",
1983
+ " .groupby(\"month\", as_index=False)[\"units_sold\"]\n",
1984
+ " .sum()\n",
1985
+ " .rename(columns={\"units_sold\": \"total_revenue\"})\n",
1986
+ " .sort_values(\"month\")\n",
1987
+ " )\n",
1988
+ "\n",
1989
+ "df_monthly[\"month\"] = pd.to_datetime(df_monthly[\"month\"], errors=\"coerce\").dt.strftime(\"%Y-%m-%d\")\n",
1990
+ "df_monthly.to_csv(\"synthetic_monthly_revenue_series.csv\", index=False)\n",
1991
+ "print(\"✅ Wrote synthetic_monthly_revenue_series.csv\")\n"
1992
+ ]
1993
+ },
1994
+ {
1995
+ "cell_type": "markdown",
1996
+ "metadata": {
1997
+ "id": "RYvGyVfXuo54"
1998
+ },
1999
+ "source": [
2000
+ "### *d. ✋🏻🛑⛔️ View the first few lines*"
2001
+ ]
2002
+ },
2003
+ {
2004
+ "cell_type": "code",
2005
+ "execution_count": null,
2006
+ "metadata": {
2007
+ "colab": {
2008
+ "base_uri": "https://localhost:8080/"
2009
+ },
2010
+ "id": "xfE8NMqOurKo",
2011
+ "outputId": "191730ba-d5e2-4df7-97d2-99feb0b704af"
2012
+ },
2013
+ "outputs": [
2014
+ {
2015
+ "output_type": "stream",
2016
+ "name": "stdout",
2017
+ "text": [
2018
+ " title sentiment_label \\\n",
2019
+ "0 A Light in the Attic neutral \n",
2020
+ "1 A Light in the Attic neutral \n",
2021
+ "2 A Light in the Attic neutral \n",
2022
+ "3 A Light in the Attic neutral \n",
2023
+ "4 A Light in the Attic neutral \n",
2024
+ "\n",
2025
+ " review_text rating popularity_score \n",
2026
+ "0 Had potential that went unrealized. Three 3 \n",
2027
+ "1 The themes were solid, but not well explored. Three 3 \n",
2028
+ "2 It simply lacked that emotional punch. Three 3 \n",
2029
+ "3 Serviceable but not something I'd go out of my... Three 3 \n",
2030
+ "4 Standard fare with some promise. Three 3 \n"
2031
+ ]
2032
+ }
2033
+ ],
2034
+ "source": []
2035
+ },
2036
+ {
2037
+ "cell_type": "code",
2038
+ "source": [
2039
+ "len(df_reviews)"
2040
+ ],
2041
+ "metadata": {
2042
+ "id": "BYVLGLBlm5jn",
2043
+ "outputId": "c4f444a5-3638-456a-c357-53a9a7fd2c60",
2044
+ "colab": {
2045
+ "base_uri": "https://localhost:8080/"
2046
+ }
2047
+ },
2048
+ "execution_count": null,
2049
+ "outputs": [
2050
+ {
2051
+ "output_type": "execute_result",
2052
+ "data": {
2053
+ "text/plain": [
2054
+ "10000"
2055
+ ]
2056
+ },
2057
+ "metadata": {},
2058
+ "execution_count": 26
2059
+ }
2060
+ ]
2061
+ }
2062
+ ],
2063
+ "metadata": {
2064
+ "colab": {
2065
+ "collapsed_sections": [
2066
+ "jpASMyIQMaAq",
2067
+ "lquNYCbfL9IM",
2068
+ "0IWuNpxxYDJF",
2069
+ "oCdTsin2Yfp3",
2070
+ "T0TOeRC4Yrnn",
2071
+ "duI5dv3CZYvF",
2072
+ "qMjRKMBQZlJi",
2073
+ "p-1Pr2szaqLk",
2074
+ "SIaJUGIpaH4V",
2075
+ "pY4yCoIuaQqp",
2076
+ "n4-TaNTFgPak",
2077
+ "HnngRNTgacYt",
2078
+ "HF9F9HIzgT7Z",
2079
+ "T8AdKkmASq9a",
2080
+ "OhXbdGD5fH0c",
2081
+ "L2ak1HlcgoTe",
2082
+ "4IXZKcCSgxnq",
2083
+ "EhIjz9WohAmZ",
2084
+ "Gi4y9M9KuDWx",
2085
+ "fQhfVaDmuULT",
2086
+ "bmJMXF-Bukdm",
2087
+ "RYvGyVfXuo54"
2088
+ ],
2089
+ "provenance": []
2090
+ },
2091
+ "kernelspec": {
2092
+ "display_name": "Python 3",
2093
+ "name": "python3"
2094
+ },
2095
+ "language_info": {
2096
+ "name": "python"
2097
+ }
2098
+ },
2099
+ "nbformat": 4,
2100
+ "nbformat_minor": 0
2101
+ }