{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "4ba6aba8"
},
"source": [
"# 🤖 **Data Collection, Creation, Storage, and Processing**\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "jpASMyIQMaAq"
},
"source": [
"## **1.** 📦 Install required packages"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "f48c8f8c",
"outputId": "5eab5d3d-d345-4d7d-b644-d7c5bc55c88d"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.12/dist-packages (4.13.5)\n",
"Requirement already satisfied: pandas in /usr/local/lib/python3.12/dist-packages (2.2.2)\n",
"Requirement already satisfied: matplotlib in /usr/local/lib/python3.12/dist-packages (3.10.0)\n",
"Requirement already satisfied: seaborn in /usr/local/lib/python3.12/dist-packages (0.13.2)\n",
"Requirement already satisfied: numpy in /usr/local/lib/python3.12/dist-packages (2.0.2)\n",
"Requirement already satisfied: textblob in /usr/local/lib/python3.12/dist-packages (0.19.0)\n",
"Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.12/dist-packages (from beautifulsoup4) (2.8.3)\n",
"Requirement already satisfied: typing-extensions>=4.0.0 in /usr/local/lib/python3.12/dist-packages (from beautifulsoup4) (4.15.0)\n",
"Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.12/dist-packages (from pandas) (2.9.0.post0)\n",
"Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.12/dist-packages (from pandas) (2025.2)\n",
"Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.12/dist-packages (from pandas) (2025.3)\n",
"Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (1.3.3)\n",
"Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (0.12.1)\n",
"Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (4.62.0)\n",
"Requirement already satisfied: kiwisolver>=1.3.1 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (1.5.0)\n",
"Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (26.0)\n",
"Requirement already satisfied: pillow>=8 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (11.3.0)\n",
"Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.12/dist-packages (from matplotlib) (3.3.2)\n",
"Requirement already satisfied: nltk>=3.9 in /usr/local/lib/python3.12/dist-packages (from textblob) (3.9.1)\n",
"Requirement already satisfied: click in /usr/local/lib/python3.12/dist-packages (from nltk>=3.9->textblob) (8.3.1)\n",
"Requirement already satisfied: joblib in /usr/local/lib/python3.12/dist-packages (from nltk>=3.9->textblob) (1.5.3)\n",
"Requirement already satisfied: regex>=2021.8.3 in /usr/local/lib/python3.12/dist-packages (from nltk>=3.9->textblob) (2025.11.3)\n",
"Requirement already satisfied: tqdm in /usr/local/lib/python3.12/dist-packages (from nltk>=3.9->textblob) (4.67.3)\n",
"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"
]
}
],
"source": [
"!pip install beautifulsoup4 pandas matplotlib seaborn numpy textblob"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lquNYCbfL9IM"
},
"source": [
"## **2.** ⛏ Web-scrape all book titles, prices, and ratings from books.toscrape.com"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0IWuNpxxYDJF"
},
"source": [
"### *a. Initial setup*\n",
"Define the base url of the website you will scrape as well as how and what you will scrape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "91d52125"
},
"outputs": [],
"source": [
"import requests\n",
"from bs4 import BeautifulSoup\n",
"import pandas as pd\n",
"import time\n",
"\n",
"base_url = \"https://books.toscrape.com/catalogue/page-{}.html\"\n",
"headers = {\"User-Agent\": \"Mozilla/5.0\"}\n",
"\n",
"titles, prices, ratings = [], [], []"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "oCdTsin2Yfp3"
},
"source": [
"### *b. Fill titles, prices, and ratings from the web pages*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "xqO5Y3dnYhxt"
},
"outputs": [],
"source": [
"# Loop through all 50 pages\n",
"for page in range(1, 51):\n",
" url = base_url.format(page)\n",
" response = requests.get(url, headers=headers)\n",
" soup = BeautifulSoup(response.content, \"html.parser\")\n",
" books = soup.find_all(\"article\", class_=\"product_pod\")\n",
"\n",
" for book in books:\n",
" titles.append(book.h3.a[\"title\"])\n",
" prices.append(float(book.find(\"p\", class_=\"price_color\").text[1:]))\n",
" ratings.append(book.p.get(\"class\")[1])\n",
"\n",
" time.sleep(0.5) # polite scraping delay"
]
},
{
"cell_type": "code",
"source": [
"len(titles), len(prices), len(ratings)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2RfLW0u7iksi",
"outputId": "691adf75-63ef-4c4d-9f1a-1690c5d97a5c"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(1000, 1000, 1000)"
]
},
"metadata": {},
"execution_count": 6
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "T0TOeRC4Yrnn"
},
"source": [
"### *c. ✋🏻🛑⛔️ Create a dataframe df_books that contains the now complete \"title\", \"price\", and \"rating\" objects*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "l5FkkNhUYTHh",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 204
},
"outputId": "bf5b4f2d-330c-485d-ea69-ee8302d19be0"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" title price rating\n",
"0 A Light in the Attic 51.77 Three\n",
"1 Tipping the Velvet 53.74 One\n",
"2 Soumission 50.10 One\n",
"3 Sharp Objects 47.82 Four\n",
"4 Sapiens: A Brief History of Humankind 54.23 Five"
],
"text/html": [
"\n",
"
\n",
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" title | \n",
" price | \n",
" rating | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" A Light in the Attic | \n",
" 51.77 | \n",
" Three | \n",
"
\n",
" \n",
" | 1 | \n",
" Tipping the Velvet | \n",
" 53.74 | \n",
" One | \n",
"
\n",
" \n",
" | 2 | \n",
" Soumission | \n",
" 50.10 | \n",
" One | \n",
"
\n",
" \n",
" | 3 | \n",
" Sharp Objects | \n",
" 47.82 | \n",
" Four | \n",
"
\n",
" \n",
" | 4 | \n",
" Sapiens: A Brief History of Humankind | \n",
" 54.23 | \n",
" Five | \n",
"
\n",
" \n",
"
\n",
"
\n",
"
\n",
"
\n"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "dataframe",
"variable_name": "df_books",
"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}"
}
},
"metadata": {},
"execution_count": 7
}
],
"source": [
"df_books = pd.DataFrame({\n",
" \"title\": titles,\n",
" \"price\": prices,\n",
" \"rating\": ratings\n",
"})\n",
"\n",
"df_books.head()"
]
},
{
"cell_type": "code",
"source": [
"df_books.to_csv(\"books.csv\", index=False)"
],
"metadata": {
"id": "sWumcBu3i6Mj"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!ls"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "CUPuxSv8jKoH",
"outputId": "c3dea3ed-d6e1-4e1d-f3ef-c0871abb5ecf"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"books.csv sample_data\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"df_books.to_excel(\"books.xlsx\", index=False)"
],
"metadata": {
"id": "R0qHyWBYjRb_"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!ls"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "97_5ABW5jdgH",
"outputId": "0ef22899-9c10-4d95-d829-9380b7163c76"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"books.csv books.xlsx sample_data\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"df_books.tail()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 204
},
"id": "alqXE8F1jpg1",
"outputId": "ea3e5ffa-e112-4c95-bfcf-97998f3389c6"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" title price rating\n",
"995 Alice in Wonderland (Alice's Adventures in Won... 55.53 One\n",
"996 Ajin: Demi-Human, Volume 1 (Ajin: Demi-Human #1) 57.06 Four\n",
"997 A Spy's Devotion (The Regency Spies of London #1) 16.97 Five\n",
"998 1st to Die (Women's Murder Club #1) 53.98 One\n",
"999 1,000 Places to See Before You Die 26.08 Five"
],
"text/html": [
"\n",
" \n",
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" title | \n",
" price | \n",
" rating | \n",
"
\n",
" \n",
" \n",
" \n",
" | 995 | \n",
" Alice in Wonderland (Alice's Adventures in Won... | \n",
" 55.53 | \n",
" One | \n",
"
\n",
" \n",
" | 996 | \n",
" Ajin: Demi-Human, Volume 1 (Ajin: Demi-Human #1) | \n",
" 57.06 | \n",
" Four | \n",
"
\n",
" \n",
" | 997 | \n",
" A Spy's Devotion (The Regency Spies of London #1) | \n",
" 16.97 | \n",
" Five | \n",
"
\n",
" \n",
" | 998 | \n",
" 1st to Die (Women's Murder Club #1) | \n",
" 53.98 | \n",
" One | \n",
"
\n",
" \n",
" | 999 | \n",
" 1,000 Places to See Before You Die | \n",
" 26.08 | \n",
" Five | \n",
"
\n",
" \n",
"
\n",
"
\n",
"
\n",
"
\n"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "dataframe",
"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}"
}
},
"metadata": {},
"execution_count": 12
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "duI5dv3CZYvF"
},
"source": [
"### *d. Save web-scraped dataframe either as a CSV or Excel file*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "lC1U_YHtZifh"
},
"outputs": [],
"source": [
"# 💾 Save to CSV\n",
"df_books.to_csv(\"books_data.csv\", index=False)\n",
"\n",
"# 💾 Or save to Excel\n",
"# df_books.to_excel(\"books_data.xlsx\", index=False)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qMjRKMBQZlJi"
},
"source": [
"### *e. ✋🏻🛑⛔️ View first fiew lines*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 206
},
"id": "O_wIvTxYZqCK",
"outputId": "349b36b0-c008-4fd5-d4a4-dba38ae18337"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" title price rating\n",
"0 A Light in the Attic 51.77 Three\n",
"1 Tipping the Velvet 53.74 One\n",
"2 Soumission 50.10 One\n",
"3 Sharp Objects 47.82 Four\n",
"4 Sapiens: A Brief History of Humankind 54.23 Five"
],
"text/html": [
"\n",
" \n",
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" title | \n",
" price | \n",
" rating | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" A Light in the Attic | \n",
" 51.77 | \n",
" Three | \n",
"
\n",
" \n",
" | 1 | \n",
" Tipping the Velvet | \n",
" 53.74 | \n",
" One | \n",
"
\n",
" \n",
" | 2 | \n",
" Soumission | \n",
" 50.10 | \n",
" One | \n",
"
\n",
" \n",
" | 3 | \n",
" Sharp Objects | \n",
" 47.82 | \n",
" Four | \n",
"
\n",
" \n",
" | 4 | \n",
" Sapiens: A Brief History of Humankind | \n",
" 54.23 | \n",
" Five | \n",
"
\n",
" \n",
"
\n",
"
\n",
"
\n",
"
\n"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "dataframe",
"variable_name": "df_books",
"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}"
}
},
"metadata": {},
"execution_count": 6
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "p-1Pr2szaqLk"
},
"source": [
"## **3.** 🧩 Create a meaningful connection between real & synthetic datasets"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SIaJUGIpaH4V"
},
"source": [
"### *a. Initial setup*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "-gPXGcRPuV_9"
},
"outputs": [],
"source": [
"import numpy as np\n",
"import random\n",
"from datetime import datetime\n",
"import warnings\n",
"\n",
"warnings.filterwarnings(\"ignore\")\n",
"random.seed(2025)\n",
"np.random.seed(2025)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "pY4yCoIuaQqp"
},
"source": [
"### *b. Generate popularity scores based on rating (with some randomness) with a generate_popularity_score function*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "mnd5hdAbaNjz"
},
"outputs": [],
"source": [
"def generate_popularity_score(rating):\n",
" base = {\"One\": 2, \"Two\": 3, \"Three\": 3, \"Four\": 4, \"Five\": 4}.get(rating, 3)\n",
" trend_factor = random.choices([-1, 0, 1], weights=[1, 3, 2])[0]\n",
" return int(np.clip(base + trend_factor, 1, 5))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "n4-TaNTFgPak"
},
"source": [
"### *c. ✋🏻🛑⛔️ Run the function to create a \"popularity_score\" column from \"rating\"*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "V-G3OCUCgR07",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 204
},
"outputId": "0f163540-194b-41a3-e4bf-6544088484df"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" title price rating popularity_score\n",
"0 A Light in the Attic 51.77 Three 3\n",
"1 Tipping the Velvet 53.74 One 2\n",
"2 Soumission 50.10 One 2\n",
"3 Sharp Objects 47.82 Four 4\n",
"4 Sapiens: A Brief History of Humankind 54.23 Five 3"
],
"text/html": [
"\n",
" \n",
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" title | \n",
" price | \n",
" rating | \n",
" popularity_score | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" A Light in the Attic | \n",
" 51.77 | \n",
" Three | \n",
" 3 | \n",
"
\n",
" \n",
" | 1 | \n",
" Tipping the Velvet | \n",
" 53.74 | \n",
" One | \n",
" 2 | \n",
"
\n",
" \n",
" | 2 | \n",
" Soumission | \n",
" 50.10 | \n",
" One | \n",
" 2 | \n",
"
\n",
" \n",
" | 3 | \n",
" Sharp Objects | \n",
" 47.82 | \n",
" Four | \n",
" 4 | \n",
"
\n",
" \n",
" | 4 | \n",
" Sapiens: A Brief History of Humankind | \n",
" 54.23 | \n",
" Five | \n",
" 3 | \n",
"
\n",
" \n",
"
\n",
"
\n",
"
\n",
"
\n"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "dataframe",
"variable_name": "df_books",
"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}"
}
},
"metadata": {},
"execution_count": 15
}
],
"source": [
"df_books[\"popularity_score\"] = df_books[\"rating\"].apply(generate_popularity_score)\n",
"\n",
"df_books.head()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "HnngRNTgacYt"
},
"source": [
"### *d. Decide on the sentiment_label based on the popularity score with a get_sentiment function*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "kUtWmr8maZLZ"
},
"outputs": [],
"source": [
"def get_sentiment(popularity_score):\n",
" if popularity_score <= 2:\n",
" return \"negative\"\n",
" elif popularity_score == 3:\n",
" return \"neutral\"\n",
" else:\n",
" return \"positive\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "HF9F9HIzgT7Z"
},
"source": [
"### *e. ✋🏻🛑⛔️ Run the function to create a \"sentiment_label\" column from \"popularity_score\"*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "tafQj8_7gYCG",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 204
},
"outputId": "fbb79831-c1b8-4f2d-e502-79100a8314b4"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" title price rating popularity_score \\\n",
"0 A Light in the Attic 51.77 Three 3 \n",
"1 Tipping the Velvet 53.74 One 2 \n",
"2 Soumission 50.10 One 2 \n",
"3 Sharp Objects 47.82 Four 4 \n",
"4 Sapiens: A Brief History of Humankind 54.23 Five 3 \n",
"\n",
" sentiment_label \n",
"0 neutral \n",
"1 negative \n",
"2 negative \n",
"3 positive \n",
"4 neutral "
],
"text/html": [
"\n",
" \n",
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" title | \n",
" price | \n",
" rating | \n",
" popularity_score | \n",
" sentiment_label | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" A Light in the Attic | \n",
" 51.77 | \n",
" Three | \n",
" 3 | \n",
" neutral | \n",
"
\n",
" \n",
" | 1 | \n",
" Tipping the Velvet | \n",
" 53.74 | \n",
" One | \n",
" 2 | \n",
" negative | \n",
"
\n",
" \n",
" | 2 | \n",
" Soumission | \n",
" 50.10 | \n",
" One | \n",
" 2 | \n",
" negative | \n",
"
\n",
" \n",
" | 3 | \n",
" Sharp Objects | \n",
" 47.82 | \n",
" Four | \n",
" 4 | \n",
" positive | \n",
"
\n",
" \n",
" | 4 | \n",
" Sapiens: A Brief History of Humankind | \n",
" 54.23 | \n",
" Five | \n",
" 3 | \n",
" neutral | \n",
"
\n",
" \n",
"
\n",
"
\n",
"
\n",
"
\n"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "dataframe",
"variable_name": "df_books",
"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}"
}
},
"metadata": {},
"execution_count": 17
}
],
"source": [
"df_books[\"sentiment_label\"] = df_books[\"popularity_score\"].apply(get_sentiment)\n",
"\n",
"df_books.head()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "T8AdKkmASq9a"
},
"source": [
"## **4.** 📈 Generate synthetic book sales data of 18 months"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "OhXbdGD5fH0c"
},
"source": [
"### *a. Create a generate_sales_profit function that would generate sales patterns based on sentiment_label (with some randomness)*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "qkVhYPXGbgEn"
},
"outputs": [],
"source": [
"def generate_sales_profile(sentiment):\n",
" months = pd.date_range(end=datetime.today(), periods=18, freq=\"M\")\n",
"\n",
" if sentiment == \"positive\":\n",
" base = random.randint(200, 300)\n",
" trend = np.linspace(base, base + random.randint(20, 60), len(months))\n",
" elif sentiment == \"negative\":\n",
" base = random.randint(20, 80)\n",
" trend = np.linspace(base, base - random.randint(10, 30), len(months))\n",
" else: # neutral\n",
" base = random.randint(80, 160)\n",
" trend = np.full(len(months), base + random.randint(-10, 10))\n",
"\n",
" seasonality = 10 * np.sin(np.linspace(0, 3 * np.pi, len(months)))\n",
" noise = np.random.normal(0, 5, len(months))\n",
" monthly_sales = np.clip(trend + seasonality + noise, a_min=0, a_max=None).astype(int)\n",
"\n",
" return list(zip(months.strftime(\"%Y-%m\"), monthly_sales))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "L2ak1HlcgoTe"
},
"source": [
"### *b. Run the function as part of building sales_data*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "SlJ24AUafoDB"
},
"outputs": [],
"source": [
"sales_data = []\n",
"for _, row in df_books.iterrows():\n",
" records = generate_sales_profile(row[\"sentiment_label\"])\n",
" for month, units in records:\n",
" sales_data.append({\n",
" \"title\": row[\"title\"],\n",
" \"month\": month,\n",
" \"units_sold\": units,\n",
" \"sentiment_label\": row[\"sentiment_label\"]\n",
" })"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4IXZKcCSgxnq"
},
"source": [
"### *c. ✋🏻🛑⛔️ Create a df_sales DataFrame from sales_data*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "wcN6gtiZg-ws",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 204
},
"outputId": "0923c679-8e5d-4b12-889f-181996aa4973"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" title month units_sold sentiment_label\n",
"0 A Light in the Attic 2024-09 100 neutral\n",
"1 A Light in the Attic 2024-10 109 neutral\n",
"2 A Light in the Attic 2024-11 102 neutral\n",
"3 A Light in the Attic 2024-12 107 neutral\n",
"4 A Light in the Attic 2025-01 108 neutral"
],
"text/html": [
"\n",
" \n",
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" title | \n",
" month | \n",
" units_sold | \n",
" sentiment_label | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" A Light in the Attic | \n",
" 2024-09 | \n",
" 100 | \n",
" neutral | \n",
"
\n",
" \n",
" | 1 | \n",
" A Light in the Attic | \n",
" 2024-10 | \n",
" 109 | \n",
" neutral | \n",
"
\n",
" \n",
" | 2 | \n",
" A Light in the Attic | \n",
" 2024-11 | \n",
" 102 | \n",
" neutral | \n",
"
\n",
" \n",
" | 3 | \n",
" A Light in the Attic | \n",
" 2024-12 | \n",
" 107 | \n",
" neutral | \n",
"
\n",
" \n",
" | 4 | \n",
" A Light in the Attic | \n",
" 2025-01 | \n",
" 108 | \n",
" neutral | \n",
"
\n",
" \n",
"
\n",
"
\n",
"
\n",
"
\n"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "dataframe",
"variable_name": "df_sales",
"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}"
}
},
"metadata": {},
"execution_count": 20
}
],
"source": [
"df_sales = pd.DataFrame(sales_data)\n",
"\n",
"df_sales.head()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EhIjz9WohAmZ"
},
"source": [
"### *d. Save df_sales as synthetic_sales_data.csv & view first few lines*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "MzbZvLcAhGaH",
"outputId": "a99b62dd-10e6-4ade-d482-61807d61559c"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" title month units_sold sentiment_label\n",
"0 A Light in the Attic 2024-09 100 neutral\n",
"1 A Light in the Attic 2024-10 109 neutral\n",
"2 A Light in the Attic 2024-11 102 neutral\n",
"3 A Light in the Attic 2024-12 107 neutral\n",
"4 A Light in the Attic 2025-01 108 neutral\n"
]
}
],
"source": [
"df_sales.to_csv(\"synthetic_sales_data.csv\", index=False)\n",
"\n",
"print(df_sales.head())"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "7g9gqBgQMtJn"
},
"source": [
"## **5.** 🎯 Generate synthetic customer reviews"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Gi4y9M9KuDWx"
},
"source": [
"### *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*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "b3cd2a50"
},
"outputs": [],
"source": [
"synthetic_reviews_by_sentiment = {\n",
"\n",
" \"positive\": [\n",
" \"An unforgettable story that kept me hooked until the last page.\",\n",
" \"Beautifully written and deeply moving.\",\n",
" \"A masterpiece with rich characters and a gripping plot.\",\n",
" \"Absolutely loved every chapter of this book.\",\n",
" \"The storytelling was immersive and emotionally powerful.\",\n",
" \"A brilliant read that exceeded my expectations.\",\n",
" \"Engaging from start to finish.\",\n",
" \"The author crafted a truly inspiring narrative.\",\n",
" \"One of the most satisfying reads this year.\",\n",
" \"A compelling journey filled with heart and depth.\",\n",
" \"The characters felt real and relatable.\",\n",
" \"An uplifting and rewarding experience.\",\n",
" \"The pacing and development were spot on.\",\n",
" \"A thoughtful and beautifully executed story.\",\n",
" \"Highly entertaining and well-structured.\",\n",
" \"An emotionally resonant and memorable read.\",\n",
" \"A book I would happily recommend to anyone.\",\n",
" \"The plot twists were clever and exciting.\",\n",
" \"An outstanding example of great storytelling.\",\n",
" \"Rich themes explored with sensitivity and care.\",\n",
" \"The dialogue felt natural and impactful.\",\n",
" \"A captivating story that stayed with me.\",\n",
" \"Strong writing and a satisfying conclusion.\",\n",
" \"An impressive and engaging narrative.\",\n",
" \"The emotional depth was remarkable.\",\n",
" \"A well-crafted and immersive experience.\",\n",
" \"The story flowed seamlessly.\",\n",
" \"An inspiring and meaningful book.\",\n",
" \"Exceptionally well-written.\",\n",
" \"A delightful and heartwarming read.\",\n",
" \"The characters were unforgettable.\",\n",
" \"An exciting and beautifully told story.\",\n",
" \"A powerful and moving journey.\",\n",
" \"Highly engaging and emotionally rich.\",\n",
" \"The world-building was fantastic.\",\n",
" \"A rewarding and thoughtful book.\",\n",
" \"Creative, compelling, and inspiring.\",\n",
" \"A truly enjoyable experience.\",\n",
" \"One of my favorite reads lately.\",\n",
" \"A strong and satisfying plot.\",\n",
" \"Expertly written and deeply engaging.\",\n",
" \"An absorbing and powerful story.\",\n",
" \"The narrative was captivating throughout.\",\n",
" \"A standout book in its genre.\",\n",
" \"Brilliant pacing and development.\",\n",
" \"An emotionally fulfilling read.\",\n",
" \"A deeply engaging storyline.\",\n",
" \"A fresh and exciting perspective.\",\n",
" \"Compelling and beautifully executed.\",\n",
" \"An excellent and inspiring book.\"\n",
" ],\n",
"\n",
" \"neutral\": [\n",
" \"An average read overall.\",\n",
" \"It was fine but nothing extraordinary.\",\n",
" \"Some parts were interesting, others less so.\",\n",
" \"A decent way to spend the time.\",\n",
" \"Neither great nor terrible.\",\n",
" \"The story had some strong moments.\",\n",
" \"It met my expectations but did not exceed them.\",\n",
" \"An okay book with mixed elements.\",\n",
" \"Some chapters stood out more than others.\",\n",
" \"A fairly standard storyline.\",\n",
" \"It was readable but not remarkable.\",\n",
" \"A moderate and balanced experience.\",\n",
" \"The pacing felt uneven at times.\",\n",
" \"A reasonable read for the genre.\",\n",
" \"The characters were somewhat engaging.\",\n",
" \"Not bad, but not particularly memorable.\",\n",
" \"A straightforward narrative.\",\n",
" \"It had potential but felt average.\",\n",
" \"The ending was acceptable.\",\n",
" \"An ordinary reading experience.\",\n",
" \"It held my attention in parts.\",\n",
" \"The writing was serviceable.\",\n",
" \"A mildly enjoyable book.\",\n",
" \"The themes were explored adequately.\",\n",
" \"Some predictable plot elements.\",\n",
" \"An acceptable but simple story.\",\n",
" \"It was fine for casual reading.\",\n",
" \"Nothing especially surprising.\",\n",
" \"A middle-of-the-road experience.\",\n",
" \"The plot was easy to follow.\",\n",
" \"A safe and familiar story.\",\n",
" \"It had both strengths and weaknesses.\",\n",
" \"An average addition to the genre.\",\n",
" \"Readable but not outstanding.\",\n",
" \"Somewhat engaging throughout.\",\n",
" \"It did not leave a strong impression.\",\n",
" \"A balanced but unremarkable read.\",\n",
" \"The writing was consistent.\",\n",
" \"A competent but simple narrative.\",\n",
" \"An okay book overall.\",\n",
" \"Moderately entertaining.\",\n",
" \"It had a few standout moments.\",\n",
" \"Neither disappointing nor impressive.\",\n",
" \"A fairly typical storyline.\",\n",
" \"A predictable but decent read.\",\n",
" \"The characters were adequate.\",\n",
" \"It was fine for what it was.\",\n",
" \"Some good ideas, average execution.\",\n",
" \"An acceptable reading experience.\",\n",
" \"Overall, just okay.\"\n",
" ],\n",
"\n",
" \"negative\": [\n",
" \"I struggled to stay engaged.\",\n",
" \"The plot felt confusing and disjointed.\",\n",
" \"The characters lacked depth.\",\n",
" \"It did not meet my expectations.\",\n",
" \"The pacing was slow and uneven.\",\n",
" \"I found it difficult to finish.\",\n",
" \"The story felt underdeveloped.\",\n",
" \"Disappointing overall.\",\n",
" \"The writing style did not work for me.\",\n",
" \"It lacked emotional impact.\",\n",
" \"The plot twists felt forced.\",\n",
" \"I expected much more from this book.\",\n",
" \"The narrative felt flat.\",\n",
" \"The characters were forgettable.\",\n",
" \"It was hard to stay interested.\",\n",
" \"The ending was unsatisfying.\",\n",
" \"The dialogue felt unnatural.\",\n",
" \"The story lacked direction.\",\n",
" \"Not an enjoyable experience.\",\n",
" \"It failed to capture my attention.\",\n",
" \"The themes were poorly explored.\",\n",
" \"The execution felt weak.\",\n",
" \"A frustrating read.\",\n",
" \"The structure felt messy.\",\n",
" \"I would not recommend this book.\",\n",
" \"It felt rushed and incomplete.\",\n",
" \"The story lacked coherence.\",\n",
" \"The pacing dragged too much.\",\n",
" \"The character development was weak.\",\n",
" \"It was difficult to connect with the story.\",\n",
" \"The narrative felt repetitive.\",\n",
" \"An underwhelming experience.\",\n",
" \"The writing lacked polish.\",\n",
" \"It did not deliver on its promise.\",\n",
" \"The plot felt predictable and dull.\",\n",
" \"I lost interest halfway through.\",\n",
" \"The book felt overly long.\",\n",
" \"The storyline was disappointing.\",\n",
" \"The tension never built properly.\",\n",
" \"It felt uninspired.\",\n",
" \"The emotional depth was missing.\",\n",
" \"A forgettable and weak read.\",\n",
" \"The story lacked originality.\",\n",
" \"It was not worth the time.\",\n",
" \"The characters felt shallow.\",\n",
" \"The writing was inconsistent.\",\n",
" \"The conclusion was abrupt.\",\n",
" \"The plot was hard to follow.\",\n",
" \"Overall, a disappointing book.\",\n",
" \"It simply did not work for me.\"\n",
" ]\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fQhfVaDmuULT"
},
"source": [
"### *b. Generate 10 reviews per book using random sampling from the corresponding 50*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "l2SRc3PjuTGM"
},
"outputs": [],
"source": [
"review_rows = []\n",
"for _, row in df_books.iterrows():\n",
" title = row['title']\n",
" sentiment_label = row['sentiment_label']\n",
" review_pool = synthetic_reviews_by_sentiment[sentiment_label]\n",
" sampled_reviews = random.sample(review_pool, 10)\n",
" for review_text in sampled_reviews:\n",
" review_rows.append({\n",
" \"title\": title,\n",
" \"sentiment_label\": sentiment_label,\n",
" \"review_text\": review_text,\n",
" \"rating\": row['rating'],\n",
" \"popularity_score\": row['popularity_score']\n",
" })"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "bmJMXF-Bukdm"
},
"source": [
"### *c. Create the final dataframe df_reviews & save it as synthetic_book_reviews.csv*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ZUKUqZsuumsp"
},
"outputs": [],
"source": [
"df_reviews = pd.DataFrame(review_rows)\n",
"df_reviews.to_csv(\"synthetic_book_reviews.csv\", index=False)"
]
},
{
"cell_type": "markdown",
"source": [
"### *c. inputs for R*"
],
"metadata": {
"id": "_602pYUS3gY5"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "3946e521",
"outputId": "224addb0-6f3c-4e83-b7b6-ae9213486099"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"✅ Wrote synthetic_title_level_features.csv\n",
"✅ Wrote synthetic_monthly_revenue_series.csv\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"def _safe_num(s):\n",
" return pd.to_numeric(\n",
" pd.Series(s).astype(str).str.replace(r\"[^0-9.]\", \"\", regex=True),\n",
" errors=\"coerce\"\n",
" )\n",
"\n",
"# --- Clean book metadata (price/rating) ---\n",
"df_books_r = df_books.copy()\n",
"if \"price\" in df_books_r.columns:\n",
" df_books_r[\"price\"] = _safe_num(df_books_r[\"price\"])\n",
"if \"rating\" in df_books_r.columns:\n",
" df_books_r[\"rating\"] = _safe_num(df_books_r[\"rating\"])\n",
"\n",
"df_books_r[\"title\"] = df_books_r[\"title\"].astype(str).str.strip()\n",
"\n",
"# --- Clean sales ---\n",
"df_sales_r = df_sales.copy()\n",
"df_sales_r[\"title\"] = df_sales_r[\"title\"].astype(str).str.strip()\n",
"df_sales_r[\"month\"] = pd.to_datetime(df_sales_r[\"month\"], errors=\"coerce\")\n",
"df_sales_r[\"units_sold\"] = _safe_num(df_sales_r[\"units_sold\"])\n",
"\n",
"# --- Clean reviews ---\n",
"df_reviews_r = df_reviews.copy()\n",
"df_reviews_r[\"title\"] = df_reviews_r[\"title\"].astype(str).str.strip()\n",
"df_reviews_r[\"sentiment_label\"] = df_reviews_r[\"sentiment_label\"].astype(str).str.lower().str.strip()\n",
"if \"rating\" in df_reviews_r.columns:\n",
" df_reviews_r[\"rating\"] = _safe_num(df_reviews_r[\"rating\"])\n",
"if \"popularity_score\" in df_reviews_r.columns:\n",
" df_reviews_r[\"popularity_score\"] = _safe_num(df_reviews_r[\"popularity_score\"])\n",
"\n",
"# --- Sentiment shares per title (from reviews) ---\n",
"sent_counts = (\n",
" df_reviews_r.groupby([\"title\", \"sentiment_label\"])\n",
" .size()\n",
" .unstack(fill_value=0)\n",
")\n",
"for lab in [\"positive\", \"neutral\", \"negative\"]:\n",
" if lab not in sent_counts.columns:\n",
" sent_counts[lab] = 0\n",
"\n",
"sent_counts[\"total_reviews\"] = sent_counts[[\"positive\", \"neutral\", \"negative\"]].sum(axis=1)\n",
"den = sent_counts[\"total_reviews\"].replace(0, np.nan)\n",
"sent_counts[\"share_positive\"] = sent_counts[\"positive\"] / den\n",
"sent_counts[\"share_neutral\"] = sent_counts[\"neutral\"] / den\n",
"sent_counts[\"share_negative\"] = sent_counts[\"negative\"] / den\n",
"sent_counts = sent_counts.reset_index()\n",
"\n",
"# --- Sales aggregation per title ---\n",
"sales_by_title = (\n",
" df_sales_r.dropna(subset=[\"title\"])\n",
" .groupby(\"title\", as_index=False)\n",
" .agg(\n",
" months_observed=(\"month\", \"nunique\"),\n",
" avg_units_sold=(\"units_sold\", \"mean\"),\n",
" total_units_sold=(\"units_sold\", \"sum\"),\n",
" )\n",
")\n",
"\n",
"# --- Title-level features (join sales + books + sentiment) ---\n",
"df_title = (\n",
" sales_by_title\n",
" .merge(df_books_r[[\"title\", \"price\", \"rating\"]], on=\"title\", how=\"left\")\n",
" .merge(sent_counts[[\"title\", \"share_positive\", \"share_neutral\", \"share_negative\", \"total_reviews\"]],\n",
" on=\"title\", how=\"left\")\n",
")\n",
"\n",
"df_title[\"avg_revenue\"] = df_title[\"avg_units_sold\"] * df_title[\"price\"]\n",
"df_title[\"total_revenue\"] = df_title[\"total_units_sold\"] * df_title[\"price\"]\n",
"\n",
"df_title.to_csv(\"synthetic_title_level_features.csv\", index=False)\n",
"print(\"✅ Wrote synthetic_title_level_features.csv\")\n",
"\n",
"# --- Monthly revenue series (proxy: units_sold * price) ---\n",
"monthly_rev = (\n",
" df_sales_r.merge(df_books_r[[\"title\", \"price\"]], on=\"title\", how=\"left\")\n",
")\n",
"monthly_rev[\"revenue\"] = monthly_rev[\"units_sold\"] * monthly_rev[\"price\"]\n",
"\n",
"df_monthly = (\n",
" monthly_rev.dropna(subset=[\"month\"])\n",
" .groupby(\"month\", as_index=False)[\"revenue\"]\n",
" .sum()\n",
" .rename(columns={\"revenue\": \"total_revenue\"})\n",
" .sort_values(\"month\")\n",
")\n",
"# if revenue is all NA (e.g., missing price), fallback to units_sold as a teaching proxy\n",
"if df_monthly[\"total_revenue\"].notna().sum() == 0:\n",
" df_monthly = (\n",
" df_sales_r.dropna(subset=[\"month\"])\n",
" .groupby(\"month\", as_index=False)[\"units_sold\"]\n",
" .sum()\n",
" .rename(columns={\"units_sold\": \"total_revenue\"})\n",
" .sort_values(\"month\")\n",
" )\n",
"\n",
"df_monthly[\"month\"] = pd.to_datetime(df_monthly[\"month\"], errors=\"coerce\").dt.strftime(\"%Y-%m-%d\")\n",
"df_monthly.to_csv(\"synthetic_monthly_revenue_series.csv\", index=False)\n",
"print(\"✅ Wrote synthetic_monthly_revenue_series.csv\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "RYvGyVfXuo54"
},
"source": [
"### *d. ✋🏻🛑⛔️ View the first few lines*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "xfE8NMqOurKo",
"outputId": "191730ba-d5e2-4df7-97d2-99feb0b704af"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" title sentiment_label \\\n",
"0 A Light in the Attic neutral \n",
"1 A Light in the Attic neutral \n",
"2 A Light in the Attic neutral \n",
"3 A Light in the Attic neutral \n",
"4 A Light in the Attic neutral \n",
"\n",
" review_text rating popularity_score \n",
"0 Had potential that went unrealized. Three 3 \n",
"1 The themes were solid, but not well explored. Three 3 \n",
"2 It simply lacked that emotional punch. Three 3 \n",
"3 Serviceable but not something I'd go out of my... Three 3 \n",
"4 Standard fare with some promise. Three 3 \n"
]
}
],
"source": []
},
{
"cell_type": "code",
"source": [
"len(df_reviews)"
],
"metadata": {
"id": "BYVLGLBlm5jn",
"outputId": "c4f444a5-3638-456a-c357-53a9a7fd2c60",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"10000"
]
},
"metadata": {},
"execution_count": 26
}
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [
"jpASMyIQMaAq",
"lquNYCbfL9IM",
"0IWuNpxxYDJF",
"oCdTsin2Yfp3",
"T0TOeRC4Yrnn",
"duI5dv3CZYvF",
"qMjRKMBQZlJi",
"p-1Pr2szaqLk",
"SIaJUGIpaH4V",
"pY4yCoIuaQqp",
"n4-TaNTFgPak",
"HnngRNTgacYt",
"HF9F9HIzgT7Z",
"T8AdKkmASq9a",
"OhXbdGD5fH0c",
"L2ak1HlcgoTe",
"4IXZKcCSgxnq",
"EhIjz9WohAmZ",
"Gi4y9M9KuDWx",
"fQhfVaDmuULT",
"bmJMXF-Bukdm",
"RYvGyVfXuo54"
],
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}