Lab 2: Advanced Visualization with Plotly

Course: INS-605: Data Analysis II
Lecturer: Sothea HAS, PhD


Objective: In this lab, we will use the cleaned Amazon Product Reviews dataset from Lab 1 to practice interactive visualization with Plotly. You will build individual figures first, then combine selected figures into a small dashboard-style view.

The lab focuses on:

The notebook of the lab can be downloaded from Lab2: Advanced Data Visualization.



0. Setup and Prepare the Data

We use the same Kaggle Amazon Product Reviews dataset as Lab 1 a. If you already have the cleaned data DataFrame from Lab 1, you may reuse it or revise it as we go through the questions below.

For this lab, the important columns are:

UserId, ProductId, Score, Time, ProfileName, Summary, and the engineered fields such as date, year, and month.

Code
# If data is already available from Lab 1, keep this cell simple.
# Otherwise, run the following code.

# %pip install kagglehub

import kagglehub
import pandas as pd
import os

path = kagglehub.dataset_download("arhamrumi/amazon-product-reviews")
file_name = [f for f in os.listdir(path) if f.endswith(".csv")][0]
data = pd.read_csv(f"{path}/{file_name}")

data.head()

0.1. Quick preparation

Use the cleaning work from Lab 1. At minimum, make sure Time is converted to a datetime column named date.

Question A.1. How many observations and variables are available for visualization?

# To do

A.2. This data contains more detailed information that you can inspect.

  • Drop duplications before and after removing column ProductId. What do you observe?
  • Group the data by ProductId and UserId, then compute the count the size of each group and sort them in descending order.
  • Inspect the summary, text, score and time of those reviews. What do you think?
  • Drop duplicated reviews for each product and keep only those with largest number of HelpfulnessDenominator.

1. First Interactive Plot: Rating Distribution

A dashboard often starts with a simple overview of the target variable.

Question B. What is the distribution of review ratings (Score)?

Create a chart showing the number of reviews for each rating.

Requirements:

  • x-axis: rating;
  • y-axis: number of reviews;
  • display the count on the bars;
  • add a meaningful title;
  • include a useful hover tooltip.

Hint: px.bar(), .value_counts(), .reset_index(), text=, hover_data=.

# To do

1.1. Improve the interaction

A useful interactive chart should help the viewer understand the data without reading the code.

Question C. Modify your figure so that:

  1. the x-axis is labeled Rating;
  2. the y-axis is labeled Number of Reviews;
  3. the bar labels show the review counts;
  4. the hover tooltip shows both rating and review count.

Hint: fig.update_layout(), fig.update_traces(), texttemplate, hovertemplate.

# To do

2. Product Comparison

Product managers may want to know which products receive the most reviews.

First calculate the top 10 products by number of reviews.

Question D.

  • Which 10 products receive the most reviews?
  • Create a horizontal bar chart.
  • Sort the products from highest to lowest review count.

Hint: groupby(), .size(), .sort_values(), .head(), px.bar(..., orientation="h").

Tip

Visualization hint: Product IDs are categorical labels. A horizontal bar chart is usually easier to read than a vertical chart when labels are long and there is no size constaint. Otherwise, vertical bar chart can also be use with rotated ticks (tickanlge = ...).

# To do

2.1. Add another dimension

The number of reviews alone does not tell us whether customers are satisfied.

For the top 10 reviewed products, calculate: - number of reviews; - average rating.

Question E. Build a scatter plot where: - x = number of reviews; - y = average rating; - each point represents a product; - point size represents review volume; - hover information includes the product ID.

Hint: groupby().agg(), px.scatter(), size=, hover_name=.

# To do

3. Customer View

A dashboard may also focus on customer activity.

Question F. Find the top 15 customers by number of reviews and create a horizontal bar chart.

Then ask:

Are the most active customers necessarily the most satisfied?

Hint: groupby("UserId").agg(...), sort_values(), px.bar().

# To do

3.1. Compare activity and satisfaction

Create a customer-level summary containing: - number of reviews; - average rating.

Question G. Create a scatter plot with: - x = number of reviews; - y = average rating; - one point per customer.

Use a logarithmic x-axis if the number of reviews is highly skewed.

Hint: px.scatter(), fig.update_xaxes(type="log").

Note

Do not assume that a high number of reviews means a customer is highly satisfied. Let the visualization show the relationship.

# To do

4. Rating Trend Over Time

A dashboard should help identify changes in customer behavior.

Convert Time to a monthly date and calculate: - number of reviews per month; - average rating per month.

Question H. Create a line chart of monthly review volume.

Then create a second line showing the monthly average rating.

Hint: pd.to_datetime(), .dt.to_period("M"), groupby(), px.line().

# To do

4.1. Make the trend easier to interpret

Add: - markers to the line; - a clear title; - axis labels; - hover information.

Question I. Are there periods with unusually high or low review activity?

Hint: markers=True, hover_data=, update_layout().

# To do

5. Rating Composition Over Time

A single average rating can hide important changes in the rating composition.

Create a monthly table containing the number of 1-, 2-, 3-, 4-, and 5-star reviews.

Question J. Create a 100% stacked area chart showing how the rating composition changes over time.

The five ratings at each month should sum to approximately 100%.

Hint: - pd.crosstab(); - div(..., axis=0); - px.area(); - groupnorm="percent" can be useful when using a long-form table.

Warning

Do not plot the raw counts and call them percentages. Convert the monthly counts to proportions or percentages first.

# To do

6. Assemble a Small Dashboard

You have now built several individual views. A dashboard combines related views so that a user can answer several questions at once.

Build a compact Overview Dashboard using make_subplots() with:

  • KPI 1: Total reviews
  • KPI 2: Unique customers
  • KPI 3: Unique products
  • KPI 4: Average rating
  • Chart 1: Rating distribution
  • Chart 2: Top 10 products
  • Chart 3: Monthly review activity

Question K. What information should a manager understand from the dashboard in less than 10 seconds?

Hint: go.Indicator(), go.Bar(), go.Scatter(), make_subplots(), add_trace(), update_layout().

# To do

6.1. Final dashboard improvements

Improve your dashboard with at least three of the following:

Question L. Which design change makes your dashboard easier to read, and why?

# To do

7. Reflection

Answer briefly:

  1. Which visualization was most useful for understanding the dataset?
  2. Which chart would you show to a product manager?
  3. Which chart would you show to a marketing manager?
  4. What is one limitation of your dashboard?
Tip

The goal is not to make the dashboard as complicated as possible. A good dashboard communicates a small number of important ideas clearly.