Weighted Average Rating
Problem
You are given a DataFrame `rating_bucket` with columns `bucket_id`, `stars`, and `respondents`. `bucket_id` is unique. Instead of one row per survey response, the data is compressed: each row says that `respondents` people all gave the same `stars` rating.
Compute the average rating across all individual respondents, rounded to 2 decimal places, and return it in a single column named `avg_rating`.
Input data
Example rows — the live problem includes the full dataset.
| bucket_id | stars | respondents |
|---|---|---|
| 1 | 5 | 10 |
| 2 | 4 | 6 |
| 3 | 3 | 3 |
| 4 | 1 | 1 |
Expected output
Your answer should return 1 row with the columns avg_rating.
Starter code (Pandas (Python))
import pandas as pd
def weighted_average_rating(rating_bucket) -> pd.DataFrame:
# Your code here
return rating_bucketSolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock