NathalieEsauB commited on
Commit
016cfcb
·
verified ·
1 Parent(s): ededb44

Upload 2 files

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